[Connecting two mainwindows in Qt]
-
Hello guys,
I hope you're all doing well.
So I've tried some examples on how to open another window (dialog) from a mainwindow but now I want to do the reverse thing.
I've developed an application Where I have a mainwindow.ui that appears first once I run my program. The user can rotate an object either manually by inserting quaternion values or through importing a csv file that contains quaternion values.
But I wanna add another mainwindow that appears before this one, add a push button that takes the user to the other mainwindow where he can see the object orientation.So When the dialog appears once I run my program, I click on the push button " take me to mainwindow", the mainwindow appears and then disappears immediately.
Can someone please tell me what's wrong with my program:
Here is the code:
My main.cpp:
#include "mainwindow.h" #include "secdialog.h" #include <QApplication> int main(int argc, char *argv[]) { // QApplication a(argc, argv); // MainWindow w; // w.show(); QApplication ab (argc, argv); SecDialog secdialog; secdialog.show(); return ab.exec(); }
My SecDialog.cpp:
#include "mainwindow.h" SecDialog::SecDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SecDialog) { ui->setupUi(this); } SecDialog::~SecDialog() { delete ui; } void SecDialog::on_pushButton_clicked() { MainWindow w; w.show(); //window.setModal(true); }
Thank you all.
-
@appdev
You must not declareMainWindow w;
as a local variable inside a function likeon_pushButton_clicked()
--- it goes out of scope and is destroyed as soon as that function exits.Your
SecDialog secdialog;
might easiest be placed as a member variable ofMainWindow
(there are other ways). Then you can writeconnect()
s to slots there whichshow()
whichever of the two windows you want shown andhide()
the other one. Or you might giveSecDialog secdialog;
the same scope/lifetime as yourMainWindow w;
back inmain()
, and connect whatever signals from them to code whichshow()
s/hide()
s the two windows appropriately.As a separate point, quite why you would want multiple
QMainWindow
s is beyond me: I would only ever have one instance of a main window in an application. I don't understand your explanation/reasoning/intention, and I don't think you further explaining it would mean much to me! :) -
Okay but in the tutorial where he opens a second dialog from a mainwindow he does this, he used a new SecDialog inside the pushbutton
Mainwindow.cpp:
void MainWindow::on_pushButton_clicked() { secDialog = new SecDialog(this); secDialog->show(); }
and it works for him, without getting errors or using signals and slots.
He's opening a second dialog from a mainwindow. I want to do the reverse thing.I do have a mainwindow that has push buttons and a widget in which there is an OpenGL scene. But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow. that's my intention.
-
@appdev said in [Connecting two mainwindows in Qt]:
and it works for him
Because in this code SecDialog is allocated on the heap, not stack. You need to understand the difference.
If you allocate an object on the stack in a function/method then this object does only exist as long as the function/method is beeing executed! When the function/method finishes all variables allocated on the stack are destroyed! -
@appdev said in [Connecting two mainwindows in Qt]:
But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow.
Sounds like a
QDialog
. And that will be easier to code (won't need signals/slots) to show, accept input, disappear and then move onto main window than what you had with a secondary non-dialog window.