How to create a window (widget) when a button is pressed in qt

I developed a GUI through the Qt creator on Linux. This project consists of some fields, text editing and some buttons.

When I click the button, I want to display another window. Is there a GUI option for this or any hard code?

+4
source share
2 answers

You need signals and slots.

You need to connect the selected signal to the user slot you created in the main widget.

Corrected code based on comments by Patrice Bernassola and Job .

In the class definition (.h file) add the lines:

Q_OBJECT private slots: void exampleButtonClicked(); private: QDialog *exampleDialog; 

The Q_OBJECT macro is needed when you define signals or slots in your classes.

The variable exampleDialog must be declared in the definition file in order to access it in the slot.

And you should initialize it, this is usually done in the constructor

 ExampleClass::ExampleClass() { //Setup you UI dialog = new QDialog; } 

In the class implementation (.cpp file) add code that does what you want, in this case create a new window.

 void ExampleClass::exampleButtonClicked() { exampleDialog->show(); } 

And also you must connect the signal to the slot using the line:

 connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked())); 

Your question is basic, so I suggest reading a basic tutorial, so you can make progress faster by avoiding the expectation of answers. Some links to tutorials that were useful to me:

http://zetcode.com/tutorials/qt4tutorial/

http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html

+8
source

when you click a button, you create another widget and show. another option is a hidden widget, http://doc.trolltech.com/4.6/qstackedwidget.html

0
source

Source: https://habr.com/ru/post/1312156/


All Articles