Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Showing my mainwindow from a controller class
Forum Updated to NodeBB v4.3 + New Features

Showing my mainwindow from a controller class

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxmotor
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bodzio131
        wrote on last edited by
        #3

        Controller(); //you create temporary object here, it's deleted before you run main loop :), should be something like Controller c;

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxmotor
          wrote on last edited by
          #4

          Thank you both of you! ;)

          Controller c; and it worked.

          Thank you so much for your time!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved