How to switch windows / menus / qwidgets normally?
-
Hello folks,
I met with Qt several months ago, made several small study projects, practice a little bit with small test programms. Now I'm still student and decided to learn this tech (Qt C++) well, registered here.
So... my first question here will be how to switch windows in app normally (in good way : ) )?
I have 2 QWidgets for example:
Widget #1:
.h #include <QWidget> #include <QPushButton> #include <QLabel> #include <QVBoxLayout> class WidgetOne : public QWidget { Q_OBJECT private: public: explicit WidgetOne(QWidget *parent = nullptr); ~WidgetOne(); public slots: void switchToWidgetTwo(); }; .cpp #include "widgetone.h" #include "widgettwo.h" //------------------------------------------------------ WidgetOne::WidgetOne(QWidget *parent) : QWidget(parent) { auto wLayout = new QVBoxLayout(this); auto button = new QPushButton("Switch to widget #2.", this); wLayout->addWidget(new QLabel("Welcome to widget #1.", this)); wLayout->addWidget(button); setLayout(wLayout); connect(button, SIGNAL(clicked()), this, SLOT(switchToWidgetTwo())); } //------------------------------------------------------ WidgetOne::~WidgetOne() { /* Nothing */ } //------------------------------------------------------ void WidgetOne::switchToWidgetTwo() { auto newWindow = new WidgetTwo(); newWindow->show(); close(); } //------------------------------------------------------
Widget #2:
.h #include <QWidget> #include <QPushButton> #include <QLabel> #include <QVBoxLayout> class WidgetTwo : public QWidget { Q_OBJECT private: public: explicit WidgetTwo(QWidget *parent = nullptr); ~WidgetTwo(); public slots: void switchToWidgetOne(); }; .cpp #include "widgettwo.h" #include "widgetone.h" //------------------------------------------------------ WidgetTwo::WidgetTwo(QWidget *parent) : QWidget(parent) { auto wLayout = new QVBoxLayout(this); auto button = new QPushButton("Switch to widget #1.", this); wLayout->addWidget(new QLabel("Welcome to widget #2.", this)); wLayout->addWidget(button); setLayout(wLayout); connect(button, SIGNAL(clicked()), this, SLOT(switchToWidgetOne())); } //------------------------------------------------------ WidgetTwo::~WidgetTwo() { /* Nothing */ } //------------------------------------------------------ void WidgetTwo::switchToWidgetOne() { auto newWindow = new WidgetOne(); newWindow->show(); close(); } //------------------------------------------------------
I just create new object of next window (if needed send there parameters (for example pointer to database object)), show him and close previos window.
So... I used same scheme in my test / study projects, and in my opinion this one can be not good way. Is it ok? Can u give me some advices in this issue, please?
P.S: I'm newbie, so if you point me to my bad code, or u have some suggestions about code style, I will say "Thank u so much! :) "
-
read the docs for void QWidget::activateWindow() and trigger it through the signal/slot mechanism.
-
Use signals and slot mechanism. Read documentation for more details.
-
@Glimmer You're leaking memory: you don't delete the previous window. To solve this you can set https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum attribute in each widget/window. This way it will be deleted when it is closed.
-
@dheerendra said in How to switch windows / menus / qwidgets normally?:
Use signals and slot mechanism. Read documentation for more details.
Already done. I use signal from button to call slot with creation new window. Maybe u meant some other usage?
-
Have a look at widget containers like StackedWidget (which works like a book / stack of pages that can be flipped) or TabWidget.
If you want to flip backwards and forwards between your two widgets, StackedWidget can be a good solution.
Take your MainWindow, create a StackedWidget and add your Widgets to it. Now you can switch the current widget by flipping the StackedWidgets page.
-
hopefully this can be the definitive word on this question...
class MyWindow: public QWidget { Q_OBJECT QPushButton* swap; // button to cause window switch QGridLayout* layout; // internal widget layout public: MyWindow(QWidget* parent = nullptr); signals: void clicked(); // emitted on window switch public slots: void MakeActive(); };
MyWindow::MyWindow(QWidget* parent): QWidget(parent) { swap = new QPushButton(tr("switch"), this); layout =new QGridLayout(); layout->addWidget(swap); setLayout(layout); resize(500, 300); connect(swap, &QPushButton::released, [this] { emit clicked(); }); } void MyWindow::MakeActive() { activateWindow(); // make this window active raise(); // and raise it }
int main(int argc, char** argv) { QApplication theApp(argc, argv); MyWindow* win1(new MyWindow()); MyWindow* win2(new MyWindow()); QObject::connect(win1, &MyWindow::clicked, win2, &MyWindow::MakeActive); QObject::connect(win2, &MyWindow::clicked, win1, &MyWindow::MakeActive); win1->show(); win2->show(); return theApp.exec(); }
-
@Kent-Dorfman said in How to switch windows / menus / qwidgets normally?:
hopefully this can be the definitive word on this question...
So u suggest to show every window at start and then just switch between them (make them active)?
-
Depends on what you want / need...
The example from @Kent-Dorfman is something like the "Alt + TAB" - function in MS Windows. It switches between existing windows (in his example by button, not key-shortcut) and grants the topmost window focus, so you can e.g. start to type inside this window.
Do you want to switch between windows or switch widgets inside one window?
@Glimmer said in How to switch windows / menus / qwidgets normally?:
So u suggest to show every window at start and then just switch between them (make them active)?
Depending on your content, I would replace widgets inside one window instead of throwing 2, 3, 4, 5 windows at the user, right at the beginning.
(It was just an example) -
For example, I have main navigation menu in application and some.... search in database menu (for example).
So user released button "Go to Search Menu" -> need to place this interface at screen.
In this example menus ui created by Qt Designer (this is proto) so and they are 2 qWidgets.
So I understood, that there are several ways to switch this widgets (menus with ui):
- create every widget object at start and show only one in one time (good for small apps only I guess), maybe with clean up data (clear listviews / boxes and other)
- create some common "window" and show needed interface there (and switching it there)
- my first variant - make new widget object with needed menu, send there needed parameters and destroy old widget object
So, I'm confused a little bit :D Now I started experimenting with first way. Looks not bad... maybe :D :
I made hereditary class from QApplication and set there operations with switching menus. + I made parent Menu class.
application.h
#include "menu.h" #include <QApplication> #include <QVector> class Application : public QApplication { Q_OBJECT private: QVector<Menu*> *menus; signals: /* Place for potential signals */ public: explicit Application(int argc, char *argv[]); void addMenu(Menu*); // Add menu to app void showMenu(int); // Show needed menu (now is just init first) static void switchMenu(Menu* prev, Menu* next); // Hide prev / Show next QVector<Menu*> getMenus(); // Get menus public slots: /* Place for potential slots */ };
application.cpp
#include "application.h" //----------------------------------------------------------------------------- Application::Application(int argc, char *argv[]) : QApplication(argc, argv) { menus = new QVector<Menu*>(); } //----------------------------------------------------------------------------- void Application::addMenu(Menu *menu) { menus->push_back(menu); } //----------------------------------------------------------------------------- void Application::showMenu(int num) { menus->at(num)->show(); } //----------------------------------------------------------------------------- QVector<Menu*> Application::getMenus() { return *menus; } //----------------------------------------------------------------------------- void Application::switchMenu(Menu* prev, Menu* next) { prev->hide(); next->show(); } //-----------------------------------------------------------------------------
menu.h
#include <QWidget> class Menu : public QWidget { Q_OBJECT private: /* Place for potential private methods / variables */ protected: /* Place for potential protected methods / variables */ signals: /* Place for potential signals */ public: explicit Menu(QWidget *parent = nullptr); /* Place for potential public methods */ public slots: /* Place for potential slots */ };
menu.cpp
#include "menu.h" //--------------------------------------------------- Menu::Menu(QWidget *parent) : QWidget(parent) { setAttribute(Qt::WA_DeleteOnClose); // Thanks @jsulm : ) } //---------------------------------------------------
menuone.h
#include "application.h" #include "menu.h" namespace Ui { class MenuOne; } class MenuOne : public Menu { Q_OBJECT private: Ui::MenuOne *ui; public: explicit MenuOne(Application *app, Menu *parent = nullptr); ~MenuOne(); public slots: };
menuone.cpp
#include "menuone.h" #include "ui_menuone.h" //------------------------------------------------------------------------- MenuOne::MenuOne(Application *app, Menu *parent) : Menu(parent), ui(new Ui::MenuOne) { ui->setupUi(this); connect(ui->toSecondMenuButton, &QPushButton::released, [this, app] { app->switchMenu(this, app->getMenus().at(1)); }); connect(ui->exitButton, &QPushButton::released, [app] { app->exit(); }); } //------------------------------------------------------------------------- MenuOne::~MenuOne() { delete ui; } //-------------------------------------------------------------------------
menutwo.h
#include "application.h" #include "menu.h" namespace Ui { class MenuTwo; } class MenuTwo : public Menu { Q_OBJECT private: Ui::MenuTwo *ui; public: explicit MenuTwo(Application *app, Menu *parent = nullptr); ~MenuTwo(); public slots: };
menutwo.cpp
#include "menutwo.h" #include "ui_menutwo.h" //------------------------------------------------------------------------- MenuTwo::MenuTwo(Application *app, Menu *parent) : Menu(parent), ui(new Ui::MenuTwo) { ui->setupUi(this); connect(ui->toFirstMenuButton, &QPushButton::released, [this, app] { app->switchMenu(this, app->getMenus().at(0)); }); connect(ui->exitButton, &QPushButton::released, [app] { app->exit(); }); } //------------------------------------------------------------------------- MenuTwo::~MenuTwo() { delete ui; } //-------------------------------------------------------------------------
main.cpp
#include "menu.h" #include "menuone.h" #include "menutwo.h" #include "application.h" int main(int argc, char *argv[]) { Application theApp(argc, argv); theApp.addMenu(new MenuOne(&theApp)); theApp.addMenu(new MenuTwo(&theApp)); theApp.showMenu(0); // plan to add enum for this return theApp.exec(); }
-
Inheriting from QApplication is not a good idea and not necessary in your case. Better put your widgets on a QMainWindow.
Yes, there are several ways. You can decide which way you want to use. In most cases some work well, others dont.
-
-
Both ways are possible. Again, depends on the use. You can create the widget once it's needed and delete it, when you change the widget or you create them and delete them all together with parent / main window / on program exit.
When you keep the widget alive and you switch, the widget keeps its state. So e.g. slider positions or text inputs stay the same, if yo dont reset them manually. This can be an ad- and disadvantage...
When you delete them after switching, you get a clean, new widget every time...