首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更新QDockWidget的子窗口小部件,当鼠标悬停在上面时,这些子窗口小部件已经自然发生

更新QDockWidget的子窗口小部件,当鼠标悬停在上面时,这些子窗口小部件已经自然发生
EN

Stack Overflow用户
提问于 2018-07-15 05:32:32
回答 1查看 120关注 0票数 1

我在QMainWindow中有一个QDockWidget,该QDockWidget包含一堆QUndoViews,其中包含我想要在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_())

运行此代码并注意,单击按钮后,undoview中的文本不会更改,但将鼠标悬停在dockwidget上后,文本会立即更新。

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51343544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档