Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to sent a function as an argument to a function c++ qt?
Forum Updated to NodeBB v4.3 + New Features

How to sent a function as an argument to a function c++ qt?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
5 Posts 3 Posters 548 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
    RuWex
    wrote on last edited by RuWex
    #1

    Hi:)
    I have this function:

    {statusBar()->showMessage(tr("Ready Start Testing"), 2000);}
    

    and I want t sent it as argument to that function:

    void ShowMessage(----here get the function -----)
    {
            function()
    }
    

    my questions is:
    How to declare the function?
    how to get it in the function??
    thank!!

    JonBJ 1 Reply Last reply
    0
    • R RuWex

      Hi:)
      I have this function:

      {statusBar()->showMessage(tr("Ready Start Testing"), 2000);}
      

      and I want t sent it as argument to that function:

      void ShowMessage(----here get the function -----)
      {
              function()
      }
      

      my questions is:
      How to declare the function?
      how to get it in the function??
      thank!!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @RuWex
      One way is to use std::function(). There are many examples, such as https://www.codingame.com/playgrounds/15869/c-runnable-snippets/passing-a-function-as-parameter or https://stackoverflow.com/a/52602922/489865 or https://stackoverflow.com/a/66224676/489865.

      One answer from 2011 --- I don't know if there are better ways now or if this still works --- at https://stackoverflow.com/a/6458710/489865 just boils down to:

      template<typename Functor>
      void f(Functor functor)
      {
         cout << functor(10) << endl;
      }
      
      int main() 
      {
          auto lambda = [] (int x) { cout << x * 50 << endl; return x * 100; };
          f(lambda); //pass lambda
      }
      

      which you can adapt to your case? Might be as simple as:

      template<typename Functor>
      void ShowMessage(Functor functor)
      {
          functor();
      }
      
          auto lambda = [=] () { statusBar()->showMessage(tr("Ready Start Testing"), 2000); };
          ShowMessage(lambda); //pass lambda
      

      ?

      R 1 Reply Last reply
      2
      • JonBJ JonB

        @RuWex
        One way is to use std::function(). There are many examples, such as https://www.codingame.com/playgrounds/15869/c-runnable-snippets/passing-a-function-as-parameter or https://stackoverflow.com/a/52602922/489865 or https://stackoverflow.com/a/66224676/489865.

        One answer from 2011 --- I don't know if there are better ways now or if this still works --- at https://stackoverflow.com/a/6458710/489865 just boils down to:

        template<typename Functor>
        void f(Functor functor)
        {
           cout << functor(10) << endl;
        }
        
        int main() 
        {
            auto lambda = [] (int x) { cout << x * 50 << endl; return x * 100; };
            f(lambda); //pass lambda
        }
        

        which you can adapt to your case? Might be as simple as:

        template<typename Functor>
        void ShowMessage(Functor functor)
        {
            functor();
        }
        
            auto lambda = [=] () { statusBar()->showMessage(tr("Ready Start Testing"), 2000); };
            ShowMessage(lambda); //pass lambda
        

        ?

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

        @JonB
        I did it ,
        But its make problem:(
        look:

        auto showMessageTime=[]() {statusBar()->showMessage(tr("Testing"), 2000);};
        

        and its make this Error:
        C:\Projects\mainwindow.cpp:138: error: cannot call member function 'QStatusBar* QMainWindow::statusBar() const' without object
        Do you know why?

        JonBJ S 2 Replies Last reply
        0
        • R RuWex

          @JonB
          I did it ,
          But its make problem:(
          look:

          auto showMessageTime=[]() {statusBar()->showMessage(tr("Testing"), 2000);};
          

          and its make this Error:
          C:\Projects\mainwindow.cpp:138: error: cannot call member function 'QStatusBar* QMainWindow::statusBar() const' without object
          Do you know why?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @RuWex said in How to sent a function as an argument to a function c++ qt?:

          Do you know why?

          Yes, because you didn't copy what I pasted. If you do, it will work. You decided to make a change to it, and took out something I had deliberately put in.

          1 Reply Last reply
          1
          • R RuWex

            @JonB
            I did it ,
            But its make problem:(
            look:

            auto showMessageTime=[]() {statusBar()->showMessage(tr("Testing"), 2000);};
            

            and its make this Error:
            C:\Projects\mainwindow.cpp:138: error: cannot call member function 'QStatusBar* QMainWindow::statusBar() const' without object
            Do you know why?

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

            @RuWex said in How to sent a function as an argument to a function c++ qt?:

            C:\Projects\mainwindow.cpp:138: error: cannot call member function 'QStatusBar* QMainWindow::statusBar() const' without object
            Do you know why?

            The error is quite clear. statusBar() needs to be called on a QMainWindow object. Have a look at your lambda captures and think about implicit this in method calls.

            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