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. starting qt application
Forum Update on Monday, May 27th 2025

starting qt application

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 2.1k Views
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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

    sierdzioS 1 Reply Last reply
    0
    • U user4592357

      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

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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.

      (Z(:^

      U 1 Reply Last reply
      2
      • sierdzioS sierdzio

        @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.

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @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

        jsulmJ sierdzioS JKSHJ 3 Replies Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • U user4592357

            @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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • U user4592357

              @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

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @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.

              (Z(:^

              1 Reply Last reply
              1
              • U user4592357

                @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

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @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?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                U 1 Reply Last reply
                2
                • JKSHJ JKSH

                  @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?

                  U Offline
                  U Offline
                  user4592357
                  wrote on last edited by
                  #8

                  @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()

                  sierdzioS JKSHJ 2 Replies Last reply
                  0
                  • U user4592357

                    @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()

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    @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();
                    }
                    

                    (Z(:^

                    1 Reply Last reply
                    5
                    • U user4592357

                      @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()

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by JKSH
                      #10

                      @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.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      3

                      • Login

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