【发布时间】:2022-01-11 12:21:27
【问题描述】:
所以这是我正在做的这个练习,每当我单击“create_button”时,我需要在 QGraphicsScene 中创建正方形,当我单击任何正方形时,该正方形的名称应该显示在 QLabel 中,即在 QGraphicsView 下,这就是我尝试这样做的方式。
这是我使用的窗口
这是代码
class rectangles(QtWidgets.QDialog):
def __init__(self):
super(rectangles, self).__init__()
loader = QtUiTools.QUiLoader()
self.ui = loader.load(ui_path, self)
self.variables()
self.ui.create_button.clicked.connect(self.creator)
self.scene.focusItemChanged.connect(self.nameDisplay)
def variables(self):
self.scene = QtWidgets.QGraphicsScene()
self.ui.canvas_area.setScene(self.scene)
self.current_focus_obj = QGraphicsItem.focusItem() #the problem is in here
def creator(self):
rect = self.scene.addRect(-20,-20,40,40, QPen(Qt.red), QBrush(Qt.gray))
rect.setFlag(QGraphicsItem.ItemIsMovable)
rect.setFlag(QGraphicsItem.ItemIsFocusable)
rect.setFlag(QGraphicsItem.ItemIsSelectable)
num_list.remove(num_list[0])
def nameDisplay(self):
self.ui.name_line.setText(str(self.current_focus_obj)) #Here I try to set
#the name of the
#active rectangle
#to my QLabel
if __name__ == '__main__':
rectangles_window = rectangles()
rectangles_window.ui.show()
但是,当我运行此代码时,我收到以下错误:
# Error: TypeError: file <maya console> line 32: descriptor 'focusItem' of 'PySide2.QtWidgets.QGraphicsItem' object needs an argument #
说实话,我真的不明白我应该在 focusItem() 中传递什么参数,这就是问题所在。我认为它不需要任何参数,因为它应该返回当前关注的任何可悲的对象。 这可能是一个小问题,但它现在真的放慢了我的步伐,所以如果有人可以提供帮助,我将非常感激:)
【问题讨论】:
标签: python pyqt5 qgraphicsview qgraphicsscene qgraphicsitem