python - PyQt5:属性值更改时通知

标签 python pyqt4 pyqt5

首先,请查看下面给出的代码示例。例如,我如何访问.setDisabled(...)QPushButton当属性值self.markup_points时里面QGraphicsView改变了吗?我如何使用 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)

最佳答案

为什么你认为单例是解决方案?单例是一种反模式,所以应该避免它,并且只有在某些情况下才有必要,而且它与通知更改无关,所以丢弃它。

解决方案是创建一个在发生更改时发出的信号,并将其连接到接收通知的插槽:

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)

关于python - PyQt5:属性值更改时通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160461/

相关文章:

qt - PyQT(也是 C++ QT 绑定(bind))嵌入式 urxvt 终端失去焦点

python - 在字符串列表中找到最常见的子字符串?

python - 多行为空时如何进行合并

python - PYQT4-pyodbc 驱动程序错误

python - Qt QTreeWidget 之间的拖放

python - 选择 QListView 中的所有项目并在目录更改时取消选择所有项目

python - QPixmap(图像)的图 block 具有重叠区域

python - 挂载 Session 对象的目的是什么?

python - Tkinter:检查 root 是否已被破坏?

python - 将 numpy 数组转换为 QImage 时的内存顺序行为问题