[Qt-interest] Is there a QT equivalent to MFC:s on_command_update_ui?

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Dec 15 17:18:24 CET 2009


Gabriel Ortiz wrote on Tuesday, December 15, 2009 3:18 PM:

> ...
> I did a lot of MFC programming a few years ago. As I recall it,
> on_command_update_ui was a quite handy mechanism, although the
> message routing in MFC is intricate.  

First off, "Forget messaging, think Signal/Slots"... and relax :)

Roughly the equivalent of "MFC messages" (my MFC experience is a 30 minutes browsing through an MFC with Visual Studio 6.0 in my university time, and then be done with that!) are Qt signals, connected to slots.

There are a few nice things, such as "connection my naming convention", when you create dialogs with the Designer and "compile" them with the UIC ("UI Compiler"): the uic will automagically connect signals, when it finds "slots" of the naming pattern on_[objectName]_[signalName](), but I can't find the documentation anymore since Qt 4.5 (was this feature dropped as a "hidden feature" now? I would expect it should still work...) (1)


Also http://doc.trolltech.com/4.6/signalsandslots.html is a good starting point to get some basic understanding.

That said, my "personal best practice" is to have one "updateUi()" slot (= method) which updates the entire dialog, such as:

class MyDialog : public QDialog {
  Q_OBJECT
  ...

private slots:
  // enables/disables widgets, updates combobox lists etc., given some
  // user interaction
  void updateUi();
  // updates all UI elements related to "Foo"
  void updateFooUi();
  // updates all UI elements related to "Bar"
  void updateBarUi();
  ...

private:
  // called from the c'tor, puts all widgets (values) into an initial (default) state
  void initialiseUi();
};

and

void MyDialog::updateUi() {
  // update everything
  updateFooUi();
  updateBarUi();
  ...
}

void MyDialog::updateFooUi() {
  if (someState == true) {
    ui.myButton->setEnabled(true);
    ...
  } else if (someOtherCondition) {
    ui.myCombobox->clear();
    ...
  }
}

updateBarUi() in analogy.

(In other words: there is no automatic "on_command_update_ui" in Qt, you need to wire the signals and implement the slots manually)

Now you connect every button, combobox etc. which should trigger an Ui update to either the "global" updateUi() (which updates the entire UI), or to a more specific "updateFooUi" (if you are sure that the given UI interaction should only affect "Foo" UI elements) or "updateBarUi" (only "Bar" related UI elements are to be updated.

Typically updating a UI (a dialog, even the MainWindow) is cheap when it comes to enabling/disabling elements, given some application state). But it /can/ be expensive when you need to calculate data first!

So it is a trade-off how "fine-granular" you want to have your "updateFooUi" (in the extreme case every button, checkbox etc. has its own slot, which triggers the /minimal/ UI update!), or "how convenient" and "close together" (locality of code!) you want to have your update logic. I tend to update the entire UI "in one go" (by always calling the "global" updateUi() method, since in practice updating the UI is cheap, especially for simple dialogs with a dozen UI elements or so.

Note: I usually *do* some optimisations when I use "Tab Widgets": for each tab I use a separate updateTabUi() method, and I dynamically connect/disconnect whenever the tab widget is shown or hidden. So In only update the currently visible tab! See http://qt.nokia.com/doc/4.6/qwidget.html#showEvent and similar.

Oh, and one last thing: I traditionally call my method where I connect all my button etc. signals to the updateUi slot "frenchConnection()" (http://www.imdb.com/title/tt0067116/) ;)

// called from the c'tor
void MyDialog::frenchConnection() {
  connect(ui.myButton, SIGNAL(clicked()),
              this, SLOT(updateUi()));
  connect(...);
  ...
}


Cheers, Oliver

(1)
Anyway, I was talking about http://qt.nokia.com/doc/4.4/designer-using-a-component.html#automatic-connections (but as I said, I can't find the corresponding page in the latest docs: http://qt.nokia.com/doc/4.6/ (I can't even find the documentation about Designer, qmake, uic etc. anymore there, the "Tools" index seems to have disappeared! Anyone?)

-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list