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. how to pass functions as parameters to another function

how to pass functions as parameters to another function

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.9k 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.
  • R Offline
    R Offline
    rezaMSLM
    wrote on last edited by
    #1

    I want to pass function to another and this is my code:
    .h file:

        void Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address,
                       int Number_of_Entries, int SlaveID, void (*f)());
    

    .cpp file:

    void settings::Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address,
                             int Number_of_Entries, int SlaveID, void (*f)())
    {
    
    }
    

    ERROR:

     error: [release/moc_settings.cpp] Error 1
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      The function pointer syntax seems fine.
      The error is something else in the .h file ( i would guess)
      That error means that the moc tool didnt make moc_settings.cpp

      R 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        The function pointer syntax seems fine.
        The error is something else in the .h file ( i would guess)
        That error means that the moc tool didnt make moc_settings.cpp

        R Offline
        R Offline
        rezaMSLM
        wrote on last edited by
        #3

        @mrjj
        if I comment

        , void (*f)()
        

        no error is received

        mrjjM 1 Reply Last reply
        0
        • R rezaMSLM

          @mrjj
          if I comment

          , void (*f)()
          

          no error is received

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rezaMSLM

          Is Read_Func listed as a slot ?

          I just tried same syntax here and it compiled. so its not syntax it self, i think.

          R 1 Reply Last reply
          0
          • mrjjM mrjj

            @rezaMSLM

            Is Read_Func listed as a slot ?

            I just tried same syntax here and it compiled. so its not syntax it self, i think.

            R Offline
            R Offline
            rezaMSLM
            wrote on last edited by
            #5

            @mrjj
            yes
            in private slots

            mrjjM 1 Reply Last reply
            0
            • R rezaMSLM

              @mrjj
              yes
              in private slots

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @rezaMSLM
              Just tested. got same error.
              So moc does not support functions pointers for slot parameters it seems.
              http://doc.qt.io/qt-5/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
              but you can use the typedef workaround.

              aha_1980A 1 Reply Last reply
              4
              • mrjjM mrjj

                @rezaMSLM
                Just tested. got same error.
                So moc does not support functions pointers for slot parameters it seems.
                http://doc.qt.io/qt-5/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
                but you can use the typedef workaround.

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

                @mrjj typedefs makes working with function pointers easier, anyway. Thats why I prefer them too.

                Qt has to stay free or it will die.

                mrjjM 1 Reply Last reply
                1
                • aha_1980A aha_1980

                  @mrjj typedefs makes working with function pointers easier, anyway. Thats why I prefer them too.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @aha_1980
                  Indeed, a typedef makes the code easier to read but also
                  allow to change the function signature in one place.
                  So the its a win-win in this case :)

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    rezaMSLM
                    wrote on last edited by
                    #9

                    I used typedef as mentioned in that link
                    here is the result:
                    .h file:

                    typedef void (*f)();
                    class settings : public QDialog
                    {
                    .
                    .
                    private slots:
                     void Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address,
                                       int Number_of_Entries, int SlaveID, f);
                    .
                    .
                    void test();
                    }
                    

                    .cpp:

                    void settings::on_pushButton_Minute_Read_clicked()
                    {
                        Read_Func(QModbusDataUnit::HoldingRegisters, 3, 1,CorrectID,test);//<--error
                    }
                    
                    void settings::Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address,  int Number_of_Entries, int SlaveID, f)
                    {
                        auto ReadUnit = QModbusDataUnit(reg_type, Start_Address, Number_of_Entries);
                        if(auto *reply = ModbusObject->sendReadRequest(ReadUnit, SlaveID))
                        {
                            if(!reply->isFinished())
                                connect(reply,&QModbusReply::finished,[this](){f();});
                            else{
                                //statusBar()->showMessage("error!;Reply Deleted",2000);
                                delete reply;
                            }
                        }
                        else
                         ; //  statusBar()->showMessage("error!",2000);
                    }
                    
                    void settings::test()
                    {
                        qDebug()<<"test executed";
                    }
                    

                    ERROR:

                     no matching function for call to 'settings::Read_Func(QModbusDataUnit::RegisterType, int, int, int&, <unresolved overloaded function type>)'
                         Read_Func(QModbusDataUnit::HoldingRegisters, 3, 1,CorrectID,test);
                                                                                         ^
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      Hi
                      you have test() as part of a class and hence its not a c function pointer as your
                      declaration suggest. ( the typedef)
                      So either move it outside class or make it static.
                      or use a member function pointer so that the info needed to call it is included.
                      https://isocpp.org/wiki/faq/pointers-to-members

                      1 Reply Last reply
                      2
                      • R Offline
                        R Offline
                        rezaMSLM
                        wrote on last edited by
                        #11
                        This post is deleted!
                        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