0

I am trying to generate an interactive GUI to display dependencies between different systems. My idea was using PyQt5's QPainter to paint ellipses to represent the systems, and to afterwards use a QLabel in order to add the system's name (and some other information) into the ellipse. The end result is supposed to look something like this: enter image description here

However, I seem to misunderstand the way the QMainWindow works. This is my code (without imports) so far:

class Node:
    def __init__(self, posx, posy, width, height, caption):
        self.posx = posx
        self.posy = posy
        self.width = width
        self.height = height
        self.caption = caption

# will get the node information via API from a different system in the future
def get_nodes():
    nodes = []
    some_node = Node(100, 200, 350, 200, "Central System")
    nodes.append(some_node)
    return nodes

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.title = "Generic title"
        self.top = 200
        self.left = 500
        self.width = 600
        self.height = 400
        
        self.nodes = get_nodes()
        # this is where I am attempting to generate a QLabel
        for node in self.nodes:
            label = QLabel()
            label.setText(node.caption)
            label.move(node.posx, node.posy)
            
        self.InitWindow()
        
    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()
    
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
        for node in self.nodes:
            painter.drawEllipse(node.posx, node.posy, node.width, node.height)
            
    
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec())

This results in the following: enter image description here

After extensive research, I was unable to find a solution to this problem. I have tried moving the for-loop in which I am attempting to generate the QLabels into different parts of the code, but the result remains the same. How can I display the texts inside of the nodes using QLabels? Is there a better way to do it? Or is this even possible with the approach I am following?

  • 1
    Don't try to reinvent the wheel: use Qt's [Graphics View Framework](https://doc.qt.io/qt-5/graphicsview.html). – ekhumoro May 24 '22 at 11:55
  • @ekhumoro it seems I didn't even know what to look for, so this was the answer I was looking for, thanks! – humanic_dolphin May 24 '22 at 12:12
  • 1
    @humanic_dolphin The Graphics View is certainly the right choice, but, in any case, your code didn't work because you just created labels that have no parent or reference and get also overwritten in the loop (so they just get destroyed). You should have added them to a parent, which, in the case of QMainWindow, should *always* be the [central widget](//doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework), not the main window itself; for the same reason, you shall *not* override the paint event of the main window, but that of the central widget. – musicamante May 24 '22 at 16:21

0 Answers0