How to separate functionality of tab pages from Qt main window class - qt

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]

Related

Signals and Slots Editing Buttons Are Disabled (Qt Designer 4.8)

I want to add custom slots to my design via Qt Designer (4.8), but editing buttons on "Configure Connection" window are disabled. How can I solve it?
You can only add custom signals/slots to subclasses of Qt classes.
As a demonstration of this, make a connection between pushButton and the top-level widget. When the connection dialog is shown, you will see that the right-hand edit button is now enabled. This is because the top-level widget will usually be a subclass of QWidget, QMainWindow, or QDialog that is defined by the application.
To add custom signals/slots to child widgets, you would need to use widget promotion, so that you can specify a subclass that will be supplied by your application. See this answer for how to promote widgets in PyQt.

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.

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.

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

Resources