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 Updated to NodeBB v4.3 + New Features

starting qt application

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 2.2k Views 4 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.
  • U Offline
    U Offline
    user4592357
    wrote on 12 Feb 2018, 16:25 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

    S 1 Reply Last reply 12 Feb 2018, 19:45
    0
    • U user4592357
      12 Feb 2018, 16:25

      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

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 12 Feb 2018, 19:45 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 12 Feb 2018, 20:09
      2
      • S sierdzio
        12 Feb 2018, 19:45

        @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 12 Feb 2018, 20:09 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

        J S J 3 Replies Last reply 13 Feb 2018, 05:47
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Feb 2018, 20:22 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
            12 Feb 2018, 20:09

            @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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 13 Feb 2018, 05:47 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
              12 Feb 2018, 20:09

              @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

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 13 Feb 2018, 06:12 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
                12 Feb 2018, 20:09

                @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

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 14 Feb 2018, 07:34 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 14 Feb 2018, 12:15
                2
                • J JKSH
                  14 Feb 2018, 07:34

                  @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 14 Feb 2018, 12:15 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()

                  S J 2 Replies Last reply 14 Feb 2018, 12:21
                  0
                  • U user4592357
                    14 Feb 2018, 12:15

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

                    S Offline
                    S Offline
                    sierdzio
                    Moderators
                    wrote on 14 Feb 2018, 12:21 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
                      14 Feb 2018, 12:15

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

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 16 Feb 2018, 05:33 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

                      1/10

                      12 Feb 2018, 16:25

                      • Login

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