Opening a Widget created in Designer, from a Main Window - qt

I’ve created 2 .ui files, one is a main window, the other a widget. Designer generates the 2 .header files each with QT_BEGIN_NAMESPACE around the class declaration.
The problem is, what works in opening my main window, does not work in opening the second widget window.
To display my main window, I created a class that inherits from my .ui file:
class myWindow: public QMainWindow, private Ui::uiClassWindow
setupUi(this);
That opens fine, so then to open the second widget window, I declare a generic widget object and then save it with a pointer to my Widget Ui header file:
QWidget newWidget;
setupUi(newWidget)
But setupUi resolves to my Main Window header file… How do I tell it to use the Widget’s setupUi?
Is there a better way to go about this?

Up to my knowledge, for setupUi function is defined in Ui namespace. You need to give scope (Ui) for other widget too.

The setupUi() method is created by uic from your UI file, and it's different for each compiled UI.
In your myWindow, you inherit from Ui::uiClassWindow and can use its setupUi() method without qualification. You'll need an instance of a different UI class for your newWidget:
auto widget_ui = new Ui::myWidget;
QWidget newWidget;
widget_ui->setupUi(newWidget)
You could delete the widget_ui immediately if you wanted - but usually, you'll need to keep it in order to access the children it has now created in newWidget.

Related

How can i change QMainWindow to QDialog

I am new in Qt. I have made a project in Qt.
Now I need to change the QMainWindow into QDialog.
How can I do this without deleting all my project?
If a change the base mainwindow into QDialog, it is necessary to make a new window for being main?
Depending on if you have a .ui file or not.
If so, you could edit the .ui and change the line
<widget class="QMainWindow" name="MainWindow">
To
<widget class="QDialog" name="MainWindow">
Also, think about if classes like QMenuBar, QToolBar and QStatusBar are necessary in your project. Qt Designer puts them in the .ui file by default and because they are used by QMainWindow.
After that, you should change the base class for your MainWindow. You should have something like this:
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
So you need change QMainWindow by QDialog.
#include <QDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QDialog
{
If you don't have a .ui file, the first step is no necessary (obviously :))
Of course, you will need to check if you are using any QMainWindow method which is not implemented by QDialog in order to avoid compilation error and lack of functionality.
You can freely use any type of QWidget as main. The main window is whatever you decide it to be, typically the one you create and show in your main() function. So you can freely change your base class to QDialog and it will probably just work.
However, if you used Qt Designer (either directly or through Qt Creator) to create the .ui file containing all the widgets rather than coding the interface by hand, and you don't want to go through that hassle again, it can be a bit tricky.
One way is to create a new QDialog and then simply copy-paste the designed window contents to the new window. This is the easiest approach, but you may lose some properties that you set in the designer because not everything is copied.
A more tricky, but also more powerful way is to edit the created .ui file by hand. It's just a simple XML file, after all. Look for the class attribute and change class="QMainWindow" to class="QDialog". You should also look for properties and check whether these properties are defined in QDialog by looking at the docs. If they are QMainWindow-specific, then just delete them carefully.
Of course, you should back up your project (or commit it to the source control system) prior to playing around with XML because you may screw up something so that the designed will simply obliterate parts of your files it can't parse.

Pass arguments to constructor of widget within QStackedWidget

I have a graphical application written in C++ using Qt for an embedded device, which uses a QStackedWidget holding a number of UI widgets. The UI is all designed in Qt Creator's designer tool. When the user navigates through the device's application the display to be shown at that menu option is selected from the QStackedWidget and this all works great.
I'm now wanting to pass in a pointer to some configuration which is read from file when the application starts, but I can't seem to find a way to pass this pointer as an argument into the constructor of a widget on the QStackedWidget. Can anyone help?
My current approach is to call a function I've written within the class of a widget on the QStackedWidget, which works but doesn't feel the best way to do it.
To my knowledge if you want to use custom constructors - with other kinds of arguments than just the QWidget * parent - you have to create the ui programmatically:
create your custom StackedWidget with a special constructor,
prepare the global interface using the designer,
then add the StackedWidget in the constructor of the class after the setupUi method.
The other way is to use an initialization method after the construction of the item, like you did.

QFileDialog in a non-QT app

I have an app that uses a 3rd party GUI framework, but I want to open files with using QFileDialog. I'm thinking of instantiating a subclass of QWidget that is invisible and serves the purpose of serving up the dialog.
Is there a better way to do this?
I don't see any need for an invisible widget, since the file dialog doesn't require a a parent widget in order to be shown up.
Since the dialog needs to have a Qt event loop running, you will need to either show the dialog modally using exec(), or using one of the static functions like getOpenFileName.
To use any of the widget classes, including the file dialog, you need to have an instance of QApplication, although that instance doesn't have to have its exec() method called.

QStackedWidget navigation from page to page

I think i'm having a fairly basic Qt problem, but i can't figure it out:
I have a QMainWindow which holds a QStackedWidget. All page widgets in there are seperate classes derived from QWidget.
So:
QMainWindow implements QStacked Window in one class.
All other pages inside the stacked widget are added classes and all have there own .ui filled with buttons and lists trough the Designer.
For navigating to different pages, inside the Mainwindow i have access to: ui.stackedWidget->setCurrentIndex(2);
It seems i don't have access to ui.stackedWidget on another page inside the stacked widget? I have no access to the ui.stackedWidget because Ui is a private member in the mainwindow class. (auto generated by Qt - using VS addon for adding QT4 classes)
I would like to know, how can i jump to another page in the stacked widget, when clicking on a button that belongs to another page inside this widget?
Note:
All pages are added to the StackedWidget in mainWIndow's constructor:
ui.stackedWidget->addWidget(page1Widget);
ui.stackedWidget->addWidget(page2Widget);
// etc..
Example of a button click signal-slot inside page1Widget:
connect(ui.btnViewData, SIGNAL(clicked()), this, SLOT(viewData()));
::viewData()
{
// navigate to another page here.
// note: ui.stackedWidget->setCurrentIndex(3); is not accessible here!
}
I believe that putting your connect() and viewData() functions within your QMainWindow object will solve your problem, since the main window can have access to both the signals emited by the child widgets and the QStackedWidget items.
You might need to write a Ui getter for each of your page, and then do something like
connect(page1Widget->getUi().btnViewData, SIGNAL(clicked()), this, SLOT(viewData)));
hope it helps,
cheers

How can I insert a widget into a mainwindow generated by Qt designer?

I have a Main window build with Qt Designer and I also have a widget built with Qt designer (both in a separate ui file). How can I instantiate my widget into my mainwindow at runtime?
The easiest way (using Designer) is to open your main window, drag a QWidget into it, and position/name the QWidget like you would your custom widget. Once that is done, right-click on the QWidget, and select Promote to.... A dialog will show up with the widgets it can be promoted to. At the bottom of that dialog, you can add a new widget for promotion. Type in the class name and include file information, and add that widget. Then select the entry in the list, and click the Promote button.
At the end of this process, you should be able to recompile, and your custom widget will be where you placed it in the main window.
Can't you use QMainWindow::setCentralWidget function?

Resources