Created a class inheriting from QWidget : a window with one button. I want to create another class that also inherits from QWidget and has 2 buttons.

It is necessary that when I press the first class button, I can switch to another class, but in one window.

Closed due to the fact that the essence of the question is not clear to the participants of Abyx , AseN , StateItPrimitive , aleksandr barakin , PashaPash ♦ Mar 28 '16 at 10:49 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    What does it mean to switch to another class? - Abyx
  • Yes, I can not explain. But I will try. I created a class, call it Class1. It inherits from QWidget and what it does: shows a window with one button. I created Class2 inheriting from QWidget too, which in turn shows two buttons. What I need is to go to the second Class Widget 2 by clicking on the Class 1 button while staying in the same window. - Astemir Tsechoev
  • gifmaker.cc/… - Astemir Tsechoev
  • That's it ......... - Astemir Tsechoev
  • @AstemirTsechoev I have on your link opens a cat walking on its hind legs. Insert an explanatory picture directly into the question. - PashaPash ♦

1 answer 1

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; }; 
  • Thank you so much! - Astemir Tsechoev