PDA

View Full Version : Lots of code in QMainWindow



GimpMaster
21st October 2009, 13:57
I have to preference this with I'm still a beginner/intermediate to Qt however I am beginning to do more advanced applications. One thing I'm beginning to realize is that lots of code could potentially be placed in the derived class of yours that represents the QMainWindow.

This is what I mean....Right now I have a QTabWidget and a QDockWidget. Each one of those controls get stuffed into the main Ui class so if say i had 10 buttons on each tab and I had 10 Tabs that would be 100 potentially separate functions (slots) for handling them. Same goes for the QDockWidget.

Am I wrong in this or is there a way to segment up your code so you don't end up with just one huge 5000 line file. I guess I'm coming from an MFC background where each Tab of the Tab control would be a separate derived class.

Thanks in advance!

caduel
21st October 2009, 16:03
No one prevents you from creating custom classes that represent a special button, a panel, a dock widget... you name it.

This highly recommended, because you do not want files with 50k lines of code.

GimpMaster
21st October 2009, 17:46
So just so I understand this a Tab for example....

I would create a new QWidget class of my own that would handle all the contents on that particular Tab, complete with its own form file. Then on the MainWindow form file I would "Promote To" my new QWidget class.

Is that right?

Thanks!

squidge
21st October 2009, 19:39
There's no need to promote unless you want to modify existing functionality.

For tabs however, there's nothing wrong with having each tab in a different ui file and source file. Same goes for other parts of your ui such as groupboxes. Its more modular that way. As long as your widgets have a parent, they'll be happy.

GimpMaster
21st October 2009, 19:43
Thanks,

I guess my problem is that I do not have an understanding of how I would assign a different UI file to a tab in designer (if you can)?

I'll keep looking. Thanks again.

wysota
21st October 2009, 19:59
I guess my problem is that I do not have an understanding of how I would assign a different UI file to a tab in designer (if you can)?

You can't do it from Designer. You have to make a proper widget out of each ui file and then use QTabWidget::addTab() to add them to your main widget or use promotion facilities of Designer (but you will still have to create proper widget classes from each ui file).

GimpMaster
21st October 2009, 20:00
Ok thanks.

squidge
22nd October 2009, 10:49
I create a seperate UI file for each tab, place a QWidget for each tab on the main form as a holder, and then tab->SetupUI(Widget). No promotion or subclassing necessary, and the source code and widgets for each tab is completely seperate.

Might not be the "proper" way of doing it, but hey, it works :)

wysota
22nd October 2009, 12:45
I create a seperate UI file for each tab, place a QWidget for each tab on the main form as a holder, and then tab->SetupUI(Widget). No promotion or subclassing necessary, and the source code and widgets for each tab is completely seperate.

Please don't do that. You'll encounter more problems with it than benefits. Make proper subclasses from those widgets and either use the promote facility of Designer or add tabs to the tab widget manually. It's really worth it. Otherwise once you start writing some code logic you'll again have everything in one file.

squidge
22nd October 2009, 19:58
Please don't do that. You'll encounter more problems with it than benefits. Make proper subclasses from those widgets and either use the promote facility of Designer or add tabs to the tab widget manually. It's really worth it. Otherwise once you start writing some code logic you'll again have everything in one file.
I don't think you understand what I mean. This is what I mean:



[window.cpp]
tab1->Setup(tab1holder);
tab2->Setup(tab2holder);

where tabXholder is a QWidget*

[tab1.cpp]
Tab::Tab(QWidget *Parent) {
ui->SetupUI(Parent);
connect(...)
connect(...)
}

... handlers for widgets in this tab ...


Is this style of coding not advised?

I'm not sure how to subclass in designer when all you have is a group of controls in a widget with no container, or is the first thing you do supposed to be a container before you placed the controls?

wysota
22nd October 2009, 20:47
Tell me, how come after posting 66 times on this forum you don't know what Designer is for and what it is not for?


class Tab : public QWidget {
public:
Tab(QWidget *parent = 0): QWidget(parent){
ui = new Ui::SomeUi;
ui->setupUi(this);
}
~Tab() { delete ui; }
private:
Ui::SomeUi *ui;
};

What's the point of having a class that actually does something on some other object inside its own constructor?

squidge
23rd October 2009, 07:59
I never thought of doing it that way :eek:

Why write code when designer can do it for you indeed. Thanks :) (It also looks neater)