如何将Qlabels添加到PYQT5中的QMainWindow中?
我正在尝试生成一个交互式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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论