An example of creating a window with switching between two widgets inside a single window.
In the first widget, the button switches to the second widget. On the second widget, one button shows the message, and the other switches back to the first widget. Do not forget to spread these classes across the h
and cpp
files:
#include <QWidget> #include <QVBoxLayout> class Widget1: public QWidget { Q_OBJECT public: Widget1() { pb_next.setText("Next"); connect(&pb_next, SIGNAL(clicked()), SIGNAL(next())); QVBoxLayout* layout = new QVBoxLayout(); layout->addWidget(&pb_next); setLayout(layout); } private: QPushButton pb_next; signals: void next(); }; class Widget2: public QWidget { Q_OBJECT public: Widget2() { pb_click.setText("Click me!"); connect(&pb_click, SIGNAL(clicked()), SIGNAL(my_clicked())); pb_back.setText("Back!"); connect(&pb_back, SIGNAL(clicked()), SIGNAL(back())); QVBoxLayout* layout = new QVBoxLayout(); layout->addWidget(&pb_click); layout->addWidget(&pb_back); setLayout(layout); } private: QPushButton pb_click; QPushButton pb_back; signals: void my_clicked(); void back(); }; #include <QMainWindow> #include <QStackedWidget> #include <QMessageBox> class MyMainWindow: public QMainWindow { Q_OBJECT public: MyMainWindow() { setCentralWidget(&stackedWidget); stackedWidget.addWidget(&w1); stackedWidget.addWidget(&w2); connect(&w1, SIGNAL(next()), SLOT(next())); connect(&w2, SIGNAL(back()), SLOT(back())); connect(&w2, SIGNAL(my_clicked()), SLOT(about())); } public slots: void next() { stackedWidget.setCurrentWidget(&w2); } void back() { stackedWidget.setCurrentWidget(&w1); } void about() { QMessageBox::information(this, QString(), "!!!"); } private: QStackedWidget stackedWidget; Widget1 w1; Widget2 w2; };