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. if operator breaks signal-slot
Forum Updated to NodeBB v4.3 + New Features

if operator breaks signal-slot

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 936 Views 3 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.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    if(true) makes my tim never starts... Why? If I remove if(true) then my tim again counts...

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        if(true) //remove me to work
            MainClass mainClass(QCoreApplication::instance());
    
        int ret= QCoreApplication::exec();
        return ret;
    }
    

    My tim code

    MainClass::MainClass(QObject *parent)
        : QObject(parent)
    {
        connect(&tim, &QTimer::timeout, &obj, &MyClass::mySlot);
        tim.start(1000);
    }
    
    void MyClass::mySlot(){
        static uint i;
        qDebug() << "hi" << i++;
    }
    
    Pl45m4P 1 Reply Last reply
    0
    • D DungeonLords

      @SimonSchroeder said in if operator breaks signal-slot:

      std::unique_ptr instead of a raw pointer

      In my code I use parent

      MainClass *mainClass(new MainClass(QCoreApplication::instance()));
      

      so why I should use delete or unique_ptr? Is it bad advice?

      S Offline
      S Offline
      SimonSchroeder
      wrote on last edited by
      #14

      @DungeonLords said in if operator breaks signal-slot:

      so why I should use delete or unique_ptr? Is it bad advice?

      Using a parent with Qt is perfectly fine and the preferred way of handling this.

      1 Reply Last reply
      0
      • D DungeonLords

        if(true) makes my tim never starts... Why? If I remove if(true) then my tim again counts...

        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            if(true) //remove me to work
                MainClass mainClass(QCoreApplication::instance());
        
            int ret= QCoreApplication::exec();
            return ret;
        }
        

        My tim code

        MainClass::MainClass(QObject *parent)
            : QObject(parent)
        {
            connect(&tim, &QTimer::timeout, &obj, &MyClass::mySlot);
            tim.start(1000);
        }
        
        void MyClass::mySlot(){
            static uint i;
            qDebug() << "hi" << i++;
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @DungeonLords said in if operator breaks signal-slot:

        if(true) //remove me to work
            MainClass mainClass(QCoreApplication::instance());
        

        Think about the lifetime of mainClass.

        This, by the way, has nothing to do with the if itself, any Signals & Slots, the timer or anything with Qt.
        Not even a C++ issue, because other OOP languages also have scopes in which objects are "living" and "valid" before they are destructed.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        D 1 Reply Last reply
        5
        • Pl45m4P Pl45m4

          @DungeonLords said in if operator breaks signal-slot:

          if(true) //remove me to work
              MainClass mainClass(QCoreApplication::instance());
          

          Think about the lifetime of mainClass.

          This, by the way, has nothing to do with the if itself, any Signals & Slots, the timer or anything with Qt.
          Not even a C++ issue, because other OOP languages also have scopes in which objects are "living" and "valid" before they are destructed.

          D Offline
          D Offline
          DungeonLords
          wrote on last edited by DungeonLords
          #3

          @Pl45m4 so

          if(true) MainClass *mainClass(new MainClass(QCoreApplication::instance()));
          

          is right solution for my case?

          Pl45m4P 1 Reply Last reply
          0
          • D DungeonLords

            @Pl45m4 so

            if(true) MainClass *mainClass(new MainClass(QCoreApplication::instance()));
            

            is right solution for my case?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @DungeonLords said in if operator breaks signal-slot:

            MainClass *mainClass(new MainClass(QCoreApplication::instance()));
            

            Why two instances of mainClass now?!
            [Edit: I think I've misread that. It should also work.
            I've seen two new in there. One inside the other as parent, instead of the pointer initialization]

             MainClass *mainClass = new MainClass(&a);
            

            should be good.

            Also fine:

             MainClass *mainClass;
             if ( /* condition */)
                  mainClass = new MainClass;
             int ret = QCoreApplication::exec();
             if (mainClass)
                 delete mainClass;
             return ret;
            

            But why you want to put the creation inside the if scope?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            jeremy_kJ 1 Reply Last reply
            5
            • Pl45m4P Pl45m4

              @DungeonLords said in if operator breaks signal-slot:

              MainClass *mainClass(new MainClass(QCoreApplication::instance()));
              

              Why two instances of mainClass now?!
              [Edit: I think I've misread that. It should also work.
              I've seen two new in there. One inside the other as parent, instead of the pointer initialization]

               MainClass *mainClass = new MainClass(&a);
              

              should be good.

              Also fine:

               MainClass *mainClass;
               if ( /* condition */)
                    mainClass = new MainClass;
               int ret = QCoreApplication::exec();
               if (mainClass)
                   delete mainClass;
               return ret;
              

              But why you want to put the creation inside the if scope?

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by jeremy_k
              #5

              @Pl45m4 said in if operator breaks signal-slot:

              Also fine:

               MainClass *mainClass;
              [...]
               if (mainClass)
                   delete mainClass;
              

              Except for the uninitialized pointer access when /* condition */ is false...

              Asking a question about code? http://eel.is/iso-c++/testcase/

              Pl45m4P 1 Reply Last reply
              1
              • jeremy_kJ jeremy_k

                @Pl45m4 said in if operator breaks signal-slot:

                Also fine:

                 MainClass *mainClass;
                [...]
                 if (mainClass)
                     delete mainClass;
                

                Except for the uninitialized pointer access when /* condition */ is false...

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #6

                @jeremy_k

                MainClass *mainClass = nullptr;
                [...]
                 if (mainClass != nullptr)
                     delete mainClass;
                

                :)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                aha_1980A 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @jeremy_k

                  MainClass *mainClass = nullptr;
                  [...]
                   if (mainClass != nullptr)
                       delete mainClass;
                  

                  :)

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Pl45m4 Still wrong ;)

                  MainClass *mainClass = nullptr;
                  [...]
                  delete mainClass;
                  

                  Regards

                  Qt has to stay free or it will die.

                  JonBJ Pl45m4P 2 Replies Last reply
                  1
                  • aha_1980A aha_1980

                    @Pl45m4 Still wrong ;)

                    MainClass *mainClass = nullptr;
                    [...]
                    delete mainClass;
                    

                    Regards

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #8

                    @aha_1980
                    What's wrong? delete-ing a variable which has value nullptr? That's fine (i.e. guaranteed to accept and ignore nullptr; quite different from delete-ing an uninitialized variable). Whether you choose to write that or test for nullptr and not call delete is up to you.

                    1 Reply Last reply
                    1
                    • aha_1980A aha_1980

                      @Pl45m4 Still wrong ;)

                      MainClass *mainClass = nullptr;
                      [...]
                      delete mainClass;
                      

                      Regards

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #9

                      @aha_1980 said in if operator breaks signal-slot:

                      @Pl45m4 Still wrong ;)

                      My code inits the MainClass ptr to nullptr.
                      When it's properly heap allocated with new ( => mainClass != nullptr), it deletes it at the end of the code.

                      And if that part

                      if ( /* condition */)
                            mainClass = new MainClass;
                      

                      is skipped, mainClass is still a nullptr and you don't have to delete it?!

                      Explain please, what's exactly wrong with that?!


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @aha_1980 said in if operator breaks signal-slot:

                        @Pl45m4 Still wrong ;)

                        My code inits the MainClass ptr to nullptr.
                        When it's properly heap allocated with new ( => mainClass != nullptr), it deletes it at the end of the code.

                        And if that part

                        if ( /* condition */)
                              mainClass = new MainClass;
                        

                        is skipped, mainClass is still a nullptr and you don't have to delete it?!

                        Explain please, what's exactly wrong with that?!

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @Pl45m4 There is nothing wrong - the check for != nullptr is just superfluous :)

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        Pl45m4P 1 Reply Last reply
                        5
                        • Christian EhrlicherC Christian Ehrlicher

                          @Pl45m4 There is nothing wrong - the check for != nullptr is just superfluous :)

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by Pl45m4
                          #11

                          @Christian-Ehrlicher said in if operator breaks signal-slot:

                          the check for != nullptr is just superfluous

                          Ah well... yes. Because calling delete on a nullptr doesn't do anything.
                          So one could call delete mainClass regardless.


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          1 Reply Last reply
                          2
                          • S Offline
                            S Offline
                            SimonSchroeder
                            wrote on last edited by
                            #12

                            This would be the perfect place for std::unique_ptr instead of a raw pointer (using std::make_unique<MainClass> to create the object). Then we wouldn't have all this discussion about delete... 😉

                            D 1 Reply Last reply
                            1
                            • S SimonSchroeder

                              This would be the perfect place for std::unique_ptr instead of a raw pointer (using std::make_unique<MainClass> to create the object). Then we wouldn't have all this discussion about delete... 😉

                              D Offline
                              D Offline
                              DungeonLords
                              wrote on last edited by DungeonLords
                              #13

                              @SimonSchroeder said in if operator breaks signal-slot:

                              std::unique_ptr instead of a raw pointer

                              In my code I use parent

                              MainClass *mainClass(new MainClass(QCoreApplication::instance()));
                              

                              so why I should use delete or unique_ptr? Is it bad advice?

                              S 1 Reply Last reply
                              0
                              • D DungeonLords

                                @SimonSchroeder said in if operator breaks signal-slot:

                                std::unique_ptr instead of a raw pointer

                                In my code I use parent

                                MainClass *mainClass(new MainClass(QCoreApplication::instance()));
                                

                                so why I should use delete or unique_ptr? Is it bad advice?

                                S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #14

                                @DungeonLords said in if operator breaks signal-slot:

                                so why I should use delete or unique_ptr? Is it bad advice?

                                Using a parent with Qt is perfectly fine and the preferred way of handling this.

                                1 Reply Last reply
                                0
                                • D DungeonLords has marked this topic as solved on

                                • Login

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