Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Qt, what are good ways to break up a large source file containing GUI logic?

I'm working on a project in C++ utilizing Qt 5, and it has a complex window with large number of UI elements. This window was designed using Qt Designer and is loaded from a UI file.

The pattern that I've found typical of Qt is to write all of the logic for a window's UI in its main, encompassing class; i.e. the "Single Inheritance Approach" from their documentation. For example, you have a class named QMyWindow and a corresponding qmywindow.ui file defining the layout, with QMyWindow containing all the logic for the UI, mainly inside of slot methods that utilize auto-connect (i.e. methods named something like on_objectName_signalName). For simple windows that don't have a lot of complexity, this is fine. However, for large, complex windows, the pattern of putting everything in one class begins to break down because the files get too big and disorganized.

The source file for my large, complex window is over 3000 lines long now. I've already got all of the actual functionality of the window broken into other classes, so these 3000 lines of code are mainly just doing the grunt work of the UI. Essentially, it's all stuff that makes sense to put under the purview of the window's class. There's just too damn much of it.

With other languages and GUI toolkits, I could pretty effectively break the window up into logical pieces. For example, in an iOS or OS X application, I could use an embedded controller in a storyboard file, allowing me to break up the logic amongst multiple controllers in a logical way and still have the nice WYSIWYG editing of the UI itself.

But I'm not aware of a good way to do that using Qt. There isn't anything I've found in Qt Designer that allows you to break up a single UI file into multiple pieces handled by multiple classes. If I were writing the code for creating and laying out the UI myself I could certainly do that, but the resultant code generated by qmake from a designer file creates one ui namespace and one setupUI function that creates everything within the encompassing window. I don't see how you can break things apart and continue to use auto-connect.

There are mechanisms in Qt to dynamically load several designer files and then embed them in a widget, but if I were to use that I would be a) dramatically adding to the complexity of my code, and b) losing the nice WYSIWYG editing, which I don't want to do.

Are there any good options here? I don't want to keep around a 3000+ line file that's difficult to navigate, but I don't see how I can break it up and still use Qt's tools, particularly Qt Designer, the way they were intended.

like image 897
Bri Bri Avatar asked Oct 30 '22 11:10

Bri Bri


1 Answers

3000 lines of code is not a problem, not entirely sure why you think otherwise.

At any rate, doing very complex UIs in a single "unit" is a bad idea. You should modularize that one huge UI into different UIs and compose them together.

All in all, I would advice against using the UI designer, and instead write the UI entirely in code. But even if you are set on using the designer, it is possible to register your custom widgets for use with it.

like image 162
dtech Avatar answered Nov 10 '22 20:11

dtech