gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:50:08 24 4
gpt4 key购买 nike

首先,请查看下面给出的代码示例。例如,我如何访问.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com