如何将Qlabels添加到PYQT5中的QMainWindow中?

发布于 2025-01-31 17:26:05 字数 2131 浏览 2 评论 0原文

我正在尝试生成一个交互式GUI,以在不同系统之间显示依赖项。 我的想法是使用PYQT5的Qpainter绘制椭圆形式来表示系统,然后使用Qlabel将系统的名称(和其他一些信息)添加到椭圆中。 最终结果应该看起来像这样:

但是,我似乎误解了Qmainwindow的工作方式。到目前为止,这是我的代码(没有导入):

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())

这将导致以下内容:

经过广泛的研究,我找不到解决此问题的解决方案。 我已经尝试移动试图将QLABEL生成代码不同部分的前面,但结果保持不变。 如何使用Qlabels显示节点内部的文本? 有更好的方法吗? 还是我遵循的方法甚至可以做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文