Qt c++ : Dynamic GUI from multiple .ui files - c++

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.

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);

If I understand you correctly you have to use addTab method of QTabWidget

Related

How to take user input In Qt with image background Widget view?

I want the user to enter a password to starts the Qt application. To take the input from the user I am using the QInputDialog widget.
The problem is I doesn't know how to add this widget on the above of some image showing window widget, this is my code
bool ok;
QString text = QInputDialog::getText(this,
tr("QInputDialog::getText()"),tr("Enter the password:"),
QLineEdit::password,"Qt-application", &ok);
if (ok && !text.isEmpty())
textLabel->setText(text);
The above-used method is looking very casual, can anybody share the sample code to get the password from the user so it looks something good with the proper size and alignment adjustment.
If you aren't satisfied with the way QInputDialog looks, why don't you use the form designer in Qt Creator to make your own fancy looking password input dialog?
In Qt Creator, select File -> New File or Project. Then choose the Qt template under Files and Classes and then select Qt Designer Form Class. From there, go through the wizard and it'll create an empty dialog for you (*.ui). Open the .UI file, then you can drag 'n drop your desired widgets, set properties, configure layout, etc...

(Qt C++) QPushButton setIcon() not working

I'm working on a project that has 40 checkable push buttons in a group and I want the icon of one button to change depending on a value I define. I'm not creating a new pushbutton, just changing the icon of the current one. So I'm testing it by trying to change one button's icon.
Here is my current code:
QPixmap b1d0(":/textures/blocks/textures/blocks/stone.png");
QIcon ButtonIcon(b1d0);
ui->slot_0->setIcon(ButtonIcon);
ui->slot_0->setIconSize(b1d0.rect().size());
The resource path was copied directly from my resource file so it is correct, I've messed with it like crazy but no change so...
slot_0 is my pushButton.
What did I do wrong? Or better yet, am I even allowed to change the icon of an existing pushButton?
Thanks for your time :)
If you are on Windows using MSVC as your compiler, and want to use Qt's resource system, you will need to add
Q_INIT_RESOURCE(res)
to the beginning of main(), where res is the name of your .qrc file without the ".qrc".
To get a full view of this, look at this gist:
https://gist.github.com/alexreinking/5992821

QT4 qlistview save icon position moved by hand for the next time

I'm developing an app with qt4.3 which opens a window with a bunch of icons loaded from .desktop files. It tries to emulate a file browser but without file browsing... I mean, you can double click and open an app or drag the icons to an other position not necessary attached to the grid.
I'm using a qdirmodel and a qlistview in "icon mode".
My problem is that I can't save the position of the icons once I moved them. Every time I restart the app, I get the icons loaded in the default position.
I can't find any way to know the position of an item or move an item to a position.
What I should do? Do I have any chance?
Thanks!
I'm sure, you can save item's position. You should read about save session qt.
This is official qt doc about Session Management Session Management
And also I'll advice to read qt's possibility save settings QSettings
After looking at the qlistview code I found that I can also inherit the qlistview class and convert to public a couple of protected functions which return the item position and move the item to a position. Thanks anyway!!

QT. QLabel perfomance

In my application I have a file manager similar to the standart windows explorer.
The structure is:
QScrollArea
QWidget
EFile
EFile
EFile
etc...
Each EFile widget contains 5 QLabels. So when the count of files is more than 30 I have a little delay while they are being created. I decided to create only visible files and create hidden on scroll or resize when they become visible. But it's not a solution - the delay remained (although it decreased of course).
The question is how should I modify my application to remove the delay while showing files.
The answer is that you do not compose your custom widget of QLabels. Instead, you overwrite the paint() function and draw the texts/pixmaps with QPainter. It is not as much work as it sounds. There are tons of examples for that in the Qt examples/tutorials.
If it really is a file explorer you want to implement, you should consider using a QFileSystemModel in combination with a QTreeView as shown in the example here:
http://qt-project.org/doc/qt-4.8/qfilesystemmodel.html
This will save you a lot of trouble.

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code