PyQt5:属性值更改时通知
Posted
技术标签:
【中文标题】PyQt5:属性值更改时通知【英文标题】:PyQt5: Notify when attribute's value is changed 【发布时间】:2018-11-05 18:55:19 【问题描述】:首先,请看下面给出的代码示例。例如,当QGraphicsView
内的属性self.markup_points
的值发生更改时,如何访问QPushButton
上的.setDisabled(...)
?我如何使用 pyqt 信号来实现这一点,或者......使用singleton?
class ImageView(QtWidgets.QGraphicsView):
def __init__(self, parent):
super(ImageView, self).__init__(parent)
self.markup_points = []
...
...
def set_image(self, pixmap):
foo()
def mousePressEvent(self, event):
foo()
self.markup_points.append(QtCore.QPointF(bar()))
super(ImageView, self).mousePressEvent(event)
...
def keyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
if (modifiers and modifiers & MOD_MASK == modifiers and
key > 0 and key != QtCore.Qt.Key_Control and key != QtCore.Qt.Key_Meta):
if key == 88:
self.remove_point()
def remove_point(self):
if len(self.markup_points):
self.markup_points.pop()
...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
...
self.imageView = ImageView()
self.btnLoad.clicked.connect(self._load_combination)
self.btnSkip.clicked.connect(self._skip_combination)
self.btnSave.clicked.connect(self._save_objects)
# qpushbutton that I want to access later
self.btnRemove.clicked.connect(self.imageView.remove_point)
...
def event_if_something_is_changed_in_image_view(self):
self.btnRemove.setDisabled(True)
【问题讨论】:
【参考方案1】:为什么你认为单例是解决方案?单例是一种反模式,所以应该避免它,只有在某些情况下才有必要,此外它与通知更改无关,所以丢弃它。
解决方案是创建一个在有变化时发出的信号,并将其连接到接收通知的插槽:
class ImageView(QtWidgets.QGraphicsView):
markupPointsChanged = QtCore.pyqtSignal(list) # <---
def __init__(self, parent):
super(ImageView, self).__init__(parent)
self.markup_points = []
# ...
def mousePressEvent(self, event):
foo()
self.append_point(QtCore.QPointF(bar()))
super(ImageView, self).mousePressEvent(event)
def keyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
if (modifiers and modifiers & MOD_MASK == modifiers and
key > 0 and key not in (QtCore.Qt.Key_Control, QtCore.Qt.Key_Meta)):
if key == QtCore.Qt.Key_X:
self.remove_point()
def append_point(self, p):
self.markup_points.append(p)
self.markupPointsChanged.emit(self.markup_points) # <---
def remove_point(self):
if self.markup_points:
self.markup_points.pop()
self.markupPointsChanged.emit(self.markup_points) # <---
# ...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
# ...
self.imageView = ImageView()
self.btnLoad.clicked.connect(self._load_combination)
self.btnSkip.clicked.connect(self._skip_combination)
self.btnSave.clicked.connect(self._save_objects)
self.btnRemove.clicked.connect(self.imageView.remove_point)
self.imageView.markupPointsChanged.connect(self.on_markupPointsChanged) # <---
@QtCore.pyqtSlot(list)
def on_markupPointsChanged(self, points):
print(points)
self.btnRemove.setDisabled(True)
【讨论】:
以上是关于PyQt5:属性值更改时通知的主要内容,如果未能解决你的问题,请参考以下文章