How to setup multiple screens in qt mainwindow - qt

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.

Related

How to add a UI QWidget to a UI QWidget?

Completely using QT Designer. I've created a QWidget to encapsulate a collection of controls. I now want to use this QWidget several times in the QMainWindow form.
How do I 'put' my QWidget onto the QMainWindow using QT Designer only?
Shouldn't this just be a simple drag and drop operation? What am I missing?
You can promote widgets.
Simply drag and drop QWidget into your mainwindow with QtDesigner.
Right click on it and press promote to In the dialog fill in your created widget's Class name (header should be filled in automatically if it doesn't match edit accordingly) press add and promote.
Now build and run your application and you should see your widget with collection of controls.

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

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)

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 separate functionality of tab pages from Qt main window class

In my application, I have QTabWidget with 3 pages with controls designed in it, using Qt creator 2.4.1. now I want to separate the functionality of these pages from MainWindow by creating new classes.
My question is how to access the MainWindow::ui instance in my new class ? which is declared in private.
it is a qt-desktop application and platform is qt 4.8.1/win 7.
You can not directly access ui of MainWindow in your tabs. As your QTabWidget is child of MainWindow (This is how it should be implemented and assume you did so).
But you can achieve this by Signals and Slots.You can perform operations in your MainWindow by writing slots to signals emitted from individual tabs.
OR
You can write a method on the MainWindow which will operate on it's UI. And call it from the tab widget by-
this->parent->parent->uiMethod();
// this(Tab) -> parent(QTabWidget) -> parent(MainWindow) -> [related method]

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