0

I am coding an app to solve electrical circuits and I need to model the circuit. To do so, I need to draw Resistors and other shapes on a scene, being able to move it and so on.

The thing is that I am tryng to show a resistor en the scene and I can't find the way. I am tryng by using QGraphicsPathItem but looking at the documentation I am not capable of doing it. I'll show a bit of the code I am writing to solve this part of the app:

1. The first code I show is the Resistor as it should be shown

2. The second part is the way I want to do it, but instead an ellipsis, I want to show a Resistor

### 1st Code

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import Qt

x0 = 100
y0 = 50

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.x1 = 12
        self.y1 = 33
        self.y2 = 18
        self.y3 = 15
        self.y4 = 9
        self.y5 = 3

        self.p1 = QtCore.QPoint(0, 0 + self.y1)
        self.p2 = QtCore.QPoint(0, 0 + self.y2)
        self.p3 = QtCore.QPoint(self.x1, self.y3)
        self.p4 = QtCore.QPoint(-self.x1, self.y4)
        self.p5 = QtCore.QPoint(self.x1, self.y5)
        self.p6 = QtCore.QPoint(-self.x1, -self.y5)
        self.p7 = QtCore.QPoint(self.x1, -self.y4)
        self.p8 = QtCore.QPoint(-self.x1, -self.y3)
        self.p9 = QtCore.QPoint(0, 0 - self.y2)
        self.p10 = QtCore.QPoint(0, 0 - self.y1)

        

    def draw_resistor(self,angle=0, x0=0, y0=0):
        self.x0 = x0
        self.y0 = y0
        self.label = QtWidgets.QLabel()
        self.canvas = QtGui.QPixmap(200, 100) # This is to create the canvas
        self.canvas.fill() # To set the canvas background color to white. If not, we will only see a black frame
        self.label.setPixmap(self.canvas)
        self.setCentralWidget(self.label)
        self.painter = QtGui.QPainter(self.label.pixmap())
        self.painter.translate(self.x0,self.y0) # To change the axis origin
        self.painter.rotate(angle)
        self.painter.drawLines(self.p1,self.p2,self.p2,self.p3,self.p3,self.p4,self.p4,self.p5,self.p5,
                               self.p6,self.p6,self.p7,self.p7,self.p8,self.p8,self.p9,self.p9,self.p10)
        self.painter.end()

    def rotate(self,angle=0):
        self.painter.rotate(angle)
        self.painter.drawLines(self.p1,self.p2,self.p2,self.p3,self.p3,self.p4,self.p4,self.p5,self.p5,
                               self.p6,self.p6,self.p7,self.p7,self.p8,self.p8,self.p9,self.p9,self.p10)
        self.label.update() # Research about this, it could be the key




app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
window.draw_resistor(45,x0,y0)
app.exec_()

###################
###################
###################
###################


### 2nd Code

import sys
from PyQt5.QtWidgets import QApplication, QGraphicsItem, QGraphicsPathItem, QGraphicsView, QGraphicsScene, QGraphicsEllipseItem, QLabel
from PyQt5.QtCore import Qt, QPointF, QRectF, QPoint
from PyQt5.QtGui import QPixmap, QPainter

class MovingObject(QGraphicsEllipseItem):
    def __init__(self, x, y, r):
        super().__init__(0, 0, r, r)
        self.setPos(x, y)
        self.setBrush(Qt.blue)
        self.setAcceptHoverEvents(True)

    # Mouse hover events
    def hoverEnterEvent(self, event):
        app.instance().setOverrideCursor(Qt.OpenHandCursor)

    def hoverLeaveEvent(self, event):
        app.instance().restoreOverrideCursor()

    # Mouse click events
    def mousePressEvent(self, event):
        pass

    def mouseMoveEvent(self, event):
        orig_cursor_position = event.lastScenePos()
        updated_cursor_position = event.scenePos()

        orig_position = self.scenePos()

        updated_cursor_x = updated_cursor_position.x() - orig_cursor_position.x() + orig_position.x()
        updated_cursor_y = updated_cursor_position.y() - orig_cursor_position.y() + orig_position.y()
        self.setPos(QPointF(updated_cursor_x, updated_cursor_y))

    def mouseReleaseEvent(self, event):
        print("x: {0}, y: {1}".format(self.pos().x(), self.pos().y()))


class GraphicView(QGraphicsView):
    def __init__(self):
        super().__init__()

        self.scene = QGraphicsScene()
        self.setScene(self.scene)
        self.setSceneRect(0, 0, 1200, 1000)

        self.moveObject = MovingObject(50, 50, 40)
        self.moveObject2 = MovingObject(100, 100, 100)
        self.scene.addItem(self.moveObject)
        self.scene.addItem(self.moveObject2)


app = QApplication(sys.argv)

view = GraphicView()
view.show()

sys.exit(app.exec_())
  • You say that you're trying to use QGraphicsPathItem, but there's nothing about that in your code. If you want to use a QGraphicsPathItem, why do you use a QGraphicsEllipseItem? Have you *studied* the documentation of the classes you're using, which are [QGraphicsPathItem](https://doc.qt.io/qt-5/qgraphicspathitem.html), its inherited classes ([QAbstractGraphicsShapeItem](https://doc.qt.io/qt-5/qabstractgraphicsshapeitem.html) and [QGraphicsItem](https://doc.qt.io/qt-5/qgraphicsitem.html)), and [QPainterPath](https://doc.qt.io/qt-5/qpainterpath.html)? – musicamante Dec 31 '21 at 10:22
  • Sorry that I forgot to add that. I just solved the problem, I leave it in the next answer – Dani RedRain Jan 10 '22 at 10:59

1 Answers1

0

I solved it, this is the solution:

class Resistor(QGraphicsPathItem):
    def __init__(self, x, y):
        super(Resistor, self).__init__()
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemIsFocusable, True)
        self.setAcceptHoverEvents(True)
        self.isSelected = False
        self.setPath(self.create_path())
        self.setPos(x, y)
    
    def create_path(self):
        path = QPainterPath()

        path.moveTo(0, 33)
        path.lineTo(0, 18)
        path.lineTo(12, 15)
        path.lineTo(-12, 9)
        path.lineTo(12, 3)
        path.lineTo(-12, -3)
        path.lineTo(12, -9)
        path.lineTo(-12, -15)
        path.lineTo(0, -18)
        path.lineTo(0, -33)

        return path