How do I add a QWidget designed in Qt Designer inside a designed QMainWindow? - qt

I started developing an application, I designed a empty QMainWindow wit just the menu bar. and created two new QWidgets and designed the features of my application on each.
Here is the thing: How do I add and interchange the last two QWidgets inside my QMainWindow?, so QMainWindow will show one set of features of my application at once.

Perhaps you are looking for QStackedWidget
(from Qt: The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.)
You can find it insde the Containers group
Add a StackedWidget into your MainWindow, then
Directly edit the widget at present page by Qt designer(use the upper-right arrows to switch between differetn widgets). Notice that
in StackedWidget you have to create another signal sender, ex: a
combobox to decide which widget should be shown), or
If your custom widget contains some customized and hand-crafted
funcationalites that Qt doesn't have, you might need the widget
promotion. (otherwise, skip this)

Related

How to setup multiple screens in qt mainwindow

I am developing a Hospital Management System using Qt. It has options to create Patients, edit Patients and generate Bills. I used a QMainWindow and had three buttons in the Toolbar for each of these three options. Now when user selects one of these three buttons I want to load the form corresponding to that , foreg., Patient Creation Form or Patient Edit form and so on.. In QtDesigner for QMainWindow I was able to design one form based on the center widget. How to design multiple form and load them over center widget based on user action.
Add New -> Qt Designer Form Class
Set name of your class, mark "Create form", inherit QDialog. Create your dialog using Qt Designer or do it by code and call it in MainWindow like this:
MyDialog dlg;
dlg.show();
To communicate between MainWindow and your forms (for example send new data to your MainWindow) you should use signals and slots.
If your forms should create and edit some data, you can do it modal. setModal method
you should use QStackedWidget
The QStackedWidget class provides a stack of widgets where only one
widget is visible at a time.
It's possible to manage stacked widgets by index or by their pointers.

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 to use my treeview subclass in qt designer?

I have a class myTreeView which is a subclass of QTreeView, which I am using in other widget and doing layout manually. now I want to include myTreeView in the new widget using designer so that I can avoid layout code. any suggestions/reference, how to do this ?
Place a QTreeView into your layout in Qt Designer. Right click the QTreeView, click Promote to... add a New Promoted Class definition using the form at the bottom of the dialog.
i.e. specify the base class of your derived class as QTreeView, give the widget a name, and specify where Qt Design can find the header file for your derived class.
That should allow you, at a minimum, to place your widget on the form as you lay it out. It will most likely show up as a grey empty box (much like a QWidget) on the layout however when you compile and build a project using your .ui file your widget will appear.

Need help on basic Qt structure

Hi I am a Qt beginner. I want to make something like a column of icons on the left, after click the icons different forms and results appear on the right, how can I do this? Should I choose QMainWindow or QWidget for this project?
should I choose mainwindow or widget for this
project?
If what you described are the only things present in the window, you should use a QMainWindow. If you think you will want to re-use this arrangement in the future, I'd use a QWidget. It will probably be easier to implement each set of forms as a separate QWidget (in Designer; if you're building the GUI programmatically, just add the forms to a QLayout in a QFrame).
a column of icons on the left, after click the icons different
forms and results appear on the right
For the column of icons, you should look at QListWidget. It provides a vertical list of QListWidgetItems, and the items can contain icons, and nothing else. Your main window can then connect to the list widget's currentItemChanged signal (or itemChanged or something else; there are several choices), and modify the forms in the right-hand side of the window appropriately.

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