How to sent a function as an argument to a function c++ qt?
-
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!! -
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!!@RuWex
One way is to usestd::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
?
-
@RuWex
One way is to usestd::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
?
@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? -
@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?@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.
-
@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?@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 aQMainWindow
object. Have a look at your lambda captures and think about implicitthis
in method calls.