What is the standard method for updating toolbars and menus?



  • I'm coming from an MFC environment where update handlers were written to determine if a particular feature (action) should be enabled or disabled. How do you do the same thing in Qt? For instance, if I have a QAction for saving the active document, then I want to effectively call saveAction.setEnabled(document.isModified()) whenever the action (toolbar, menu, etc) is displayed to the user.

    Is there a built in mechanism to accomplish this already? or are users expected to create a background thread to call all of these update action handlers? or is there perhaps another way of implementing this?

    Thanks


  • Lifetime Qt Champion

    Hi,

    You don't need to do anything particular. Calling setEnabled when your document is modified is enough. The state of the button will stay until you call setEnabled again with a different value. The toolbar visibility has no influence on the state of that button.



  • Unfortunately that solution does not provide the desired behavior. It would not be acceptable to be forced to manually update the action's enabled state from all the numerous code paths that may modify the document.

    Currently I've implemented the desired behavior by having an updateActionStates() method which sets the action states according to the active document and its modified value, and it is called in an overrided MainWindow::event(QEvent* event). This causes the QAction to reflect the document's modified state without having to manually invoke updateActionStates() when we think the document may have changed.

    The only problem right now is that the updateActionStates() method is likely being invoked far more often than need be, but it's still a vastly superior solution than manually invoking updateActionStates() in 60 different places.


  • Lifetime Qt Champion

    If you are talking about QTextDocument, then you have the modificationChanged property that you can use for that purpose. No need to check each and every code path. The notification changed signal will be emitted by the document itself when changed.



  • I am not dealing with a QTextDocument.


  • Lifetime Qt Champion

    In that case, it would be very useful to share a little more details about your code to be able to help you.



  • Perhaps experience with MFC programming is required to fully understand my problem. Thanks for your time.


  • Moderators

    No, experience with MFC is not required. A better description of what widgets you are using is required. What is your document which can be changed? Which Qt widget do you use to show and change it?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.