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

how to pass functions as parameters to another function

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.9k Views 1 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.
  • R Offline
    R Offline
    rezaMSLM
    wrote on 6 Jan 2019, 12:02 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Jan 2019, 12:15 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 6 Jan 2019, 12:21
      3
      • M mrjj
        6 Jan 2019, 12:15

        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 6 Jan 2019, 12:21 last edited by
        #3

        @mrjj
        if I comment

        , void (*f)()
        

        no error is received

        M 1 Reply Last reply 6 Jan 2019, 12:25
        0
        • R rezaMSLM
          6 Jan 2019, 12:21

          @mrjj
          if I comment

          , void (*f)()
          

          no error is received

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Jan 2019, 12:25 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 6 Jan 2019, 12:27
          0
          • M mrjj
            6 Jan 2019, 12:25

            @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 6 Jan 2019, 12:27 last edited by
            #5

            @mrjj
            yes
            in private slots

            M 1 Reply Last reply 6 Jan 2019, 12:33
            0
            • R rezaMSLM
              6 Jan 2019, 12:27

              @mrjj
              yes
              in private slots

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 6 Jan 2019, 12:33 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.

              A 1 Reply Last reply 6 Jan 2019, 12:44
              4
              • M mrjj
                6 Jan 2019, 12:33

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

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 6 Jan 2019, 12:44 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.

                M 1 Reply Last reply 6 Jan 2019, 12:55
                1
                • A aha_1980
                  6 Jan 2019, 12:44

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 6 Jan 2019, 12:55 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 6 Jan 2019, 13:17 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
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 6 Jan 2019, 13:31 last edited by mrjj 1 Jun 2019, 13:33
                      #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 7 Jan 2019, 05:39 last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0

                        3/11

                        6 Jan 2019, 12:21

                        8 unread
                        • Login

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