Pergunta

I have a main window UI file created in Qt Designer. To avoid clutter and make the code more modular I'd like to create some parts of this window in separate ui files.

For example I may have a tab widget on the main window and then I'd like to have separate page1.ui and page2.ui which I can then "embed" inside the main window.

Is it possible with just UI files or do I need to create a class for each additional widget?

I was thinking about creating separate classes Page1UI and Page2UI which publicly inherit from ui_page1.h and ui_page2.h generated by qmake (so that the widgets inside them can be easily accessed from the main window) and then promoting main window's placeholder widgets to those classes. So in code I'd have something like this for example:

connect (page1->ui->someButtonFromPage1uiFile, ..., this->ui->someMainWindowWidget, ...);

However Qt documentation doesn't seem to mention public inheritance so I wonder if this is the right approach or if there may be a simpler way.

Foi útil?

Solução

You don't need inheritance for this. These UI files are just widgets. So of course you can do this. You should have a main class which will have a tab widget and then just add your page1, page2, etc in main tab widget. What you need is composition here in my opinion.

What you need to do is create a separate class derived from QWidget which will contain an object of your generated UI form. You need to call setupUI() of that generated form in your new class with this as parameter.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top