更新 QDockWidget 的子部件,这在用鼠标悬停时已经自然发生
Update a QDockWidget's subwidgets which already happens naturally upon hovering over with mouse
我在 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()
时触发更新?
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_())
我在 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()
时触发更新?
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_())