Question

I have created a traditional Qt (widget based) GUI, something like this: MainWindow::MainWindow(parent) : QMainWindow(parent)

This is designed by Qt Creator as forms (mainwindow.ui), aka Design Mode. Everything works fine. But the GUI code with all widgets, initializing the corresponding models, and functionality gets quit long. I'd like to refactor to small units. Things I came up with:

  1. I tried using specialized (derived) widgets. Example: A created MyTableView::QTableView contains the specialized model, as well the signal/slot handling between model and widget. This reduces the amount of code in MainWindow. However, I do loose the capability to design the GUI via Qt Creator's Design mode.
  2. The best thing I came up with so far, was to spilt the source code (multiple cpp files). It still represents one class, but less code in one file.

So, how could I better partition my GUI class?

Was it helpful?

Solution

If you still want to uncouple the initialization of widgets by derived widgets, you can use "promote to ..." option in Qt designer. Steps:

  1. class MyTableView : public QTableView {}, and so initialization of table view is moved to the constructor of MyTableView.
  2. In Qt Designer, open the ui form (MainWidow.ui), and drag and drop a QTableView on it;
  3. Right mouse click the QTableView, in prompt menu, there's a "promote to" option, open it
  4. In the dialog of "promoting widget", specify your custom QTableView's class name and header file, say MyTableView, MyTableView.h. This step requires existing custom class and header file.

Borrowed a picture: http://developer.nokia.com/community/wiki/images/thumb/9/90/MBA_promote_to.png/800px-MBA_promote_to.png

OTHER TIPS

You could create your own Qt widgets and register them with QtDesigner. Then will you be able to use them on forms as mere QLabels and friends. See this link

In a recent project, we had pretty restrictive uncoupling requirements (especially not to be too strongly linked to Qt). What we used to do based on MVC-like pattern is:

  • Implement a controller that controls the application workflow
  • Add a GUI "adapter" class per screen that communicates with the controller. Let's say HomeScreen class, SecondScreen class
  • Each adapter class contains a given number of widgets: TimelineWidget, FormWidget
  • Each widget is composed of a ui member (Ui::TimelineWidget ui) that is generated from a .ui file designd with Qt designer

Note that this structure might not be suitable for small projects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top