starting qt application



  • my application object creates the window and show it (both happen in constructor).

    so in main() i have:

    QApplication app(...);
    
    MyAppClass c;
    
    return app.exec();
    

    i don't like this because first of all my application object is unused (but i have to create it), and i wanna know a better way. thanks


  • Moderators

    @user4592357 said in starting qt application:

    i don't like this because first of all my application object is unused

    Incorrect :-) You are starting the event loop in app.exec() call. Also, Qt does not support instantiating QObjects without a Q*Application object, so you have to have it. Also, the event loop is vital for signals and slots, and - obviously - for receiving events. You cannot skip it. I'm assuming your MyAppClass does use Qt, of course.

    Just accept that it's needed and move on :-) If you really want to get rid of these 2 innocent extra lines, you can make MyAppClass inherit from QApplication, push your GUI to some slot and execute it with QTimer::singleShot(). Then you maybe could condense your main.cpp to:

    return MyAppClass().exec();
    

    Just a guess, though, untested.



  • @sierdzio
    you have got me wrong.
    my whole concern was with the class that i created. as you can see, the object is only declared (juat because the window is created inside the ctor) and i don't like that it's only declared.

    for the sake for the word, i could call some init() function on it but i don't want it


  • Lifetime Qt Champion

    Hi,

    If MyAppClass is a QWidget it should be:

    QApplication app(...);
    
    MyAppClass c;
    c.show();
    
    return app.exec();
    

    Therefore you don't just have the declaration of an object, you also call a method on it.


  • Moderators

    @user4592357 said in starting qt application:

    my whole concern was with the class that i created. as you can see, the object is only declared (juat because the window is created inside the ctor) and i don't like that it's only declared

    What's the problem with that?


  • Moderators

    @user4592357 said in starting qt application:

    my whole concern was with the class that i created. as you can see, the object is only declared (juat because the window is created inside the ctor) and i don't like that it's only declare

    I don't get it... you created that class and you put the show() method in the constructor, right? So if you don't like that solution, implement some other :-) For example, one that @SGaist suggests.


  • Moderators

    @user4592357 said in starting qt application:

    i don't like this because first of all my application object is unused (but i have to create it)

    You don't have to create it.

    Just copy the contents of the MyAppClass constructor into main() and then remove all MyAppClass code.

    as you can see, the object is only declared (juat because the window is created inside the ctor) and i don't like that it's only declared.

    How do you write C++ code that you like?



  • @JKSH
    i didn't mean the class is useless. i meant the variable that's created in main() is unused. the whole problem is that i create window in MyAppClass because after creating it, i create window related widgets afterwards. i doon't want that code to be in main()


  • Moderators

    @user4592357 said in starting qt application:

    i doon't want that code to be in main()

    What do you want to have in main(), then? It's not magic, something has to instantiate your widgets, you know...

    Maybe you would prefer subclassing QApplicaiton? Like:

    MyAppClass : public QApplication {
    // ...
      void show() { mainWidget.show(); }
    };
    
    // main.cpp:
    int main() {
      MyAppClass app(argc, argv);
      app.show();
      return app.exec();
    }
    

  • Moderators

    @user4592357 said in starting qt application:

    i didn't mean the class is useless. i meant the variable that's created in main() is unused.

    Then give it a useful method and then call that method. @sgaist and @sierdzio have already shown you examples.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.