Reputation: 11
I have been developing an GUI application in Qt. In which i need to load the GUI from different form files (.ui). Please be patient as I am going to write a lot of things.
The idea is to keep my application completely flexible that the user can change the interface by simply editing .ui files and there will be no need to modify the source code for new element added to .ui files.
I am also trying to divide the software in multiple layers. (I'll try to clarify below.)
For example: (Please see the attached image. I have merged all the images in single file because my account is not authorised to add more than two images.)
I have the main window as show in image (Main)
The first.ui file contains primary widget which is tab widget and looks like as shown in image (First)
The second.ui file contains second widget which I need to add into the tab widget. the second form looks like as shown in image (Second)
At the end the complete interface will look something like as shown in image (Final) Image Link Here
In the code I have done the following:
/* Created a Main Window with menubar. The menubar has an action to select and open a first.ui file. The File opened is loaded by QUiLoader loader.
First.ui file containes a QTabWidget with two tabs and a label. In label, i have given a path to another file second.ui
loader loads the opened file and returns the QWidget
The returned QWidget has been set as a CenntralWidget in MainWindow.
Then I have read the first.ui file to find the text given in label (which is the path to another file second.ui).
Once I have obtained that path, I have loaded the second.ui file using loader. This file contais a widget having form layout and few buttons.
Now I am trying to add this obtained widget to the Tab loaded from previous file.
CASE 1: I successfully managed to do it by using: selectedTab->layout()->addWidget(otherWidgetObainedFromSecondFile);
but in this case, the problem is that, I cannot access the element of form and buttons. That is, The buttons and form fields doesnt look like an active element. They just look like a snapshot fixed in the tab.
CASE 2: So, in contrary I decided not to directly add the widget to tab. But i created another MainWindow, set the second widget as centralWidget to this new main window. And then added this mainwindow to tab.
but it also gave me the same result.
*/
Please suggest me a solution. I am open to different suggestions but I want to keep the idea same that is the GUI has to remain in layered form (loads from multiple ui files)
I will really appreciate your help.
Upvotes: 1
Views: 1935
Reputation: 44
You can use QFormBuilder
for this.
Assuming your Tab Widget is named TabPane
QFormBuilder loader;
QFile file(":second.ui");
file.open(QFile::ReadOnly);
QWidget *pane=loader.load(&file,this);
file.close();
qDeleteAll(ui->TabPane->findChildren<QWidget *>(QString(),Qt::FindDirectChildrenOnly)); //remove any existing child widgets from TabPane
ui->TabPane->layout()->addWidget(pane);
Upvotes: 2