Showing my mainwindow from a controller class
-
Hello Qt devs!
When running the code shown below, everything works fine.
@#include "mainmenu.h"
#include <QtGui>
#include <QApplication>
#include <QThread>int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainMenu w;
w.show();
return a.exec();
}@But - I would like to have a controller class taking care of all windows. I tried this:
@#include <QtGui>
#include <QApplication>
#include <QThread>
#include "Controller.h"int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Controller();
return a.exec();
}@My controller.cpp:
@#include "Controller.h"
Controller::Controller() {
w.show();}@
I include the "MainMenu.h" and declare "w" in my controller.h file.
This won't work for me though. Is this not allowed?
Thank you for your time!
-
I guess there is a stack/reference-initialization problem. Have you tried declaring w as a dynamic allocated memory using a pointer? The following works for me (not knowing what a MainMenu is I used a label):
@QLabel* ll = new QLabel();
QLabel& l = *ll;
l.setText("HELLO");
l.show();@I placed it in a controller class as you do and it worked.
-
Controller(); //you create temporary object here, it's deleted before you run main loop :), should be something like Controller c;
-
Thank you both of you! ;)
Controller c; and it worked.
Thank you so much for your time!