Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update a QDockWidget's subwidgets which already happens naturally upon hovering over with mouse
Forum Update on Monday, May 27th 2025

Update a QDockWidget's subwidgets which already happens naturally upon hovering over with mouse

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 217 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by
    #1

    I have a QDockWidget in a QMainWindow, the QDockWidget contains a stack of QUndoViews containing commands that I want to change the text of in the QUndoView. You accomplish this by doing command.setText(). However, the QUndoView is only showing the updated text when I hover over it with the mouse. Not even will calling the below right after setText work:

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

    Here is the minimal code to demonstrate the problem:

    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_())
    

    Run this code and notice that after clicking the button, the text in the undoview doesn't change, but after hovering over the dockwidget, the text updates immediately.

    How can I trigger the update upon command.setText()?

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved