Open a new dialog from a dialog in qt

I am trying to open a new dialog box from an existing dialog box in a button click event, but I cannot do this since I opened the dialog box from MainWindow.

I'm trying to:

Dialog1 *New = new Dialog1(); New->show(); 

Is there any other way to open the form dialog box of an existing Window dialog ???

+6
source share
3 answers

There must be some other problem because your code looks good to me. Here's how I do it:

 #include <QtGui> class Dialog : public QDialog { public: Dialog() { QDialog *subDialog = new QDialog; subDialog->setWindowTitle("Sub Dialog"); QPushButton *button = new QPushButton("Push to open new dialog", this); connect(button, SIGNAL(clicked()), subDialog, SLOT(show())); } }; class MainWindow : public QMainWindow { public: MainWindow() { Dialog *dialog = new Dialog; dialog->setWindowTitle("Dialog"); dialog->show(); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.setWindowTitle("Main Window"); w.show(); return a.exec(); } 
By the way, notice how I plugged in QPushButton "clicked" a signal into the "show" QDialog slot. Very comfortably.
+8
source

I am new to QT and I had a similar problem. In my case, I called up a new dialog from a function from the main dialog. I used dlg->show , which did not wait for the result of a new dialog. Therefore, the program is still running. I change dlg->show to dlg->exec , and now the dialog works. In your code, the dialog seems like a local variable, maybe you have the same problem. Another option would be to use a static pointer.

 Dialog1 *newDlg = new Dialog1(); this->hide(); int result = newDlg->exec(); this->show(); delete newDlg; 
+1
source

in mainwindow.h you must declare a pointer to a new dialog and enable the new dialog.h as

 #include <myNewDialog.h> private: Ui::MainWindow *ui; MyNewDialog *objMyNewDialog; 

after which you can call your dialog box, which will be displayed in mainwindow.cpp as

 void MainWindow::on_btnClose_clicked() { objMyNewDialog= new MyNewDialog(this); objMyNewDialog->show(); } 
0
source

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


All Articles