更新QDockWidget的子小部件,将其悬停在鼠标上时自然会发生

时间:2018-07-14 21:32:32

标签: python pyqt pyqt5 qdockwidget

我在QMainWindow中有一个QDockWidget,该QDockWidget包含一堆QUndoView,其中包含我想更改QUndoView中的文本的命令。您可以通过执行command.setText()完成此操作。但是,当我用鼠标悬停在QUndoView上时,它仅显示更新的文本。 setText工作后,甚至都不会立即调用以下内容:

    self.editor().undoView().setFocus()
    self.editor().undoView().update()
    self.editor().undoView().hide()
    self.editor().undoView().show()

这是演示该问题的最少代码:

from PyQt5.QtWidgets import (QMainWindow, QDockWidget, QPushButton, QLabel, \
                             QStackedWidget, QUndoStack, QUndoCommand, QApplication, \
                             QUndoView)
import sys
from PyQt5.QtCore import QObject, pyqtSignal, Qt

class Command(QUndoCommand):
    def __init__(self):
        super().__init__()

    def undo(self):
        print("Command Undone")

    def redo(self):
        print("Command Redone")


class CommandTimeline(QDockWidget):
    def __init__(self):
        super().__init__()
        self.stackedWidget = QStackedWidget()
        self.setWidget(self.stackedWidget)

    def addUndoView(self, stack):
        view = QUndoView(stack)
        self.stackedWidget.addWidget(view)
        return view


class Obj(QObject):
    nameChanged = pyqtSignal(str)

    def __init__(self, name):
        super().__init__()
        self._name = name

    def setName(self, name):
        self._name = name
        self.nameChanged.emit(name)

    def __str__(self):
        return self._name


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.undoStack = QUndoStack()
        self.commandTimeline = CommandTimeline()
        self.commandTimeline.addUndoView(self.undoStack)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.commandTimeline)
        button = QPushButton("Click")
        self.setCentralWidget(button)
        button.clicked.connect(self.changeObjName)
        self.obj = Obj("A")
        self.addCommand()

    def addCommand(self):
        def getText(obj):
            return "The command text: " + str(obj)
        command = Command()
        command.setText(getText(self.obj))
        self.obj.nameChanged.connect(lambda name: command.setText(getText(self.obj)))
        self.undoStack.push(command)

    def changeObjName(self):
        self.obj.setName("B")


if __name__ == "__main__":
    app = QApplication([])

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

运行此代码,请注意,单击按钮后,撤消视图中的文本不会更改,但是将鼠标悬停在停靠小部件上之后,文本会立即更新。

如何在command.setText()上触发更新?

1 个答案:

答案 0 :(得分:1)

QUndoCommand不会通知文本是否更改,它被设计为具有静态文本。因此,我们必须执行此任务,在这种情况下,请使用setFocus(),您表示它不起作用,但是对我来说它起作用了,因此您可能没有正确实现它,或者您的MCVE无法验证。

import sys

from functools import partial

from PyQt5.QtWidgets import (QMainWindow, QDockWidget, QPushButton, QLabel, \
                             QStackedWidget, QUndoStack, QUndoCommand, QApplication, \
                             QUndoView)
from PyQt5.QtCore import QObject, pyqtSignal, Qt

class Command(QUndoCommand):
    def undo(self):
        print("Command Undone")

    def redo(self):
        print("Command Redone")


class CommandTimeline(QDockWidget):
    def __init__(self):
        super().__init__()
        self.stackedWidget = QStackedWidget()
        self.setWidget(self.stackedWidget)

    def addUndoView(self, stack):
        view = QUndoView(stack)
        self.stackedWidget.addWidget(view)
        return view


class Obj(QObject):
    nameChanged = pyqtSignal(str)

    def __init__(self, name):
        super().__init__()
        self._name = name

    def setName(self, name):
        self._name = name
        self.nameChanged.emit(name)

    def __str__(self):
        return self._name


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.undoStack = QUndoStack()
        self.commandTimeline = CommandTimeline()
        self.view = self.commandTimeline.addUndoView(self.undoStack)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.commandTimeline)
        button = QPushButton("Click")
        self.setCentralWidget(button)
        button.clicked.connect(self.changeObjName)
        self.obj = Obj("A")
        self.addCommand()


    def addCommand(self):
        command = Command()
        command.setText("The command text: {}".format(self.obj))
        self.obj.nameChanged.connect(partial(self.onNameChanged, command))
        self.undoStack.push(command)

    def changeObjName(self):
        self.obj.setName("B")

    def onNameChanged(self, command, name):
        fw = QApplication.focusWidget()
        command.setText("The command text: {}".format(name))
        self.view.setFocus()
        fw.setFocus()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())