Pass a function with parameter
-
Reason for this post is I am trying to learn not just "cut and paste". In the following code I am trying to put a function with parameter into "addAction " function It fails with two errors I am trying to decipher and learn what I am doing wrong. I tested
Run_DDD_OPTION(testWidget); and it runs fine
QWidget \*testWidget = new QWidget(); testWidget->setWindowTitle("test widget window "); Run\_DDD\_OPTION(testWidget); runs fine QAction \*CAT\_DDD\_OPTION = CATMenu-> addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "), this, &MainWindow::Run\_DDD\_OPTION(testWidget) );
this runs as expected when no parameters are passed
QAction \*CAT\_DDD\_OPTION\_TAB = CATMenu-> addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "), this, &MainWindow::Run\_TAB );
There are too many "addAction" functions , different objects, so all I have to go by are these errors. (It woudl probably help if I knew what "addAction" expects as parameters.
/media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157: error: lvalue required as unary ‘&’ operand
../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157:32: error: lvalue required as unary ‘&’ operand
1157 | &MainWindow::Run_DDD_OPTION(testWidget)
| ~~~~~~~~~~~~~~~~~^/media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1155: error: cannot take the address of an rvalue of type 'void'
-
Reason for this post is I am trying to learn not just "cut and paste". In the following code I am trying to put a function with parameter into "addAction " function It fails with two errors I am trying to decipher and learn what I am doing wrong. I tested
Run_DDD_OPTION(testWidget); and it runs fine
QWidget \*testWidget = new QWidget(); testWidget->setWindowTitle("test widget window "); Run\_DDD\_OPTION(testWidget); runs fine QAction \*CAT\_DDD\_OPTION = CATMenu-> addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "), this, &MainWindow::Run\_DDD\_OPTION(testWidget) );
this runs as expected when no parameters are passed
QAction \*CAT\_DDD\_OPTION\_TAB = CATMenu-> addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "), this, &MainWindow::Run\_TAB );
There are too many "addAction" functions , different objects, so all I have to go by are these errors. (It woudl probably help if I knew what "addAction" expects as parameters.
/media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157: error: lvalue required as unary ‘&’ operand
../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157:32: error: lvalue required as unary ‘&’ operand
1157 | &MainWindow::Run_DDD_OPTION(testWidget)
| ~~~~~~~~~~~~~~~~~^/media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1155: error: cannot take the address of an rvalue of type 'void'
This looks suspicious to me:
&MainWindow::Run_DDD_OPTION(testWidget)
It looks like you're trying to create a pointer to whatever
Run_DDD_OPTION
returns. However, you also sayRun_DDD_OPTION(testWidget); // runs fine
which either means that it returns
void
or you didn't care what it returned. My guess is the former, which would explain all the error messages. If it returnsvoid
, you can't take its address, and there is no lvalue (=a memory address). EDIT: If you want to pass the functionRun_DDD_OPTION
as an argument to addAction, just writeaddAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
this,
MainWindow::Run_DDD_OPTION
);You may need the
&
in front of the function name--I haven't done this in a long time, so I have to look it up. --It looks like it's optional. If it doesn't work without it, try it with it.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
This looks suspicious to me:
&MainWindow::Run_DDD_OPTION(testWidget)
It looks like you're trying to create a pointer to whatever
Run_DDD_OPTION
returns. However, you also sayRun_DDD_OPTION(testWidget); // runs fine
which either means that it returns
void
or you didn't care what it returned. My guess is the former, which would explain all the error messages. If it returnsvoid
, you can't take its address, and there is no lvalue (=a memory address). EDIT: If you want to pass the functionRun_DDD_OPTION
as an argument to addAction, just writeaddAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
this,
MainWindow::Run_DDD_OPTION
);You may need the
&
in front of the function name--I haven't done this in a long time, so I have to look it up. --It looks like it's optional. If it doesn't work without it, try it with it.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Thanks I think you may have answered my question. The line is just to test validity of the function.
Run_DDD_OPTION(testWidget); // runs fine
addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
this,
MainWindow::Run_DDD_OPTION
);The above code works IF "&" is prefixed , HOWEVER I like to make that parameter of "addAction" variable, more versatile. And that is why I am having issues, hence my post.
&MainWindow::Run_DDD_OPTION
So my question would be if this works without parameter why do I need to return a pointer ? I presume it would be same as the passed parmeter type.
Run_DDD_OPTION
Now again , I just do not want to "cut and paste" without knowing why. Thanks for your contribution.
-
Thanks I think you may have answered my question. The line is just to test validity of the function.
Run_DDD_OPTION(testWidget); // runs fine
addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
this,
MainWindow::Run_DDD_OPTION
);The above code works IF "&" is prefixed , HOWEVER I like to make that parameter of "addAction" variable, more versatile. And that is why I am having issues, hence my post.
&MainWindow::Run_DDD_OPTION
So my question would be if this works without parameter why do I need to return a pointer ? I presume it would be same as the passed parmeter type.
Run_DDD_OPTION
Now again , I just do not want to "cut and paste" without knowing why. Thanks for your contribution.
I don't understand how you want to make addAction's last parameter "more versatile". You'll have to explain that.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
I don't understand how you want to make addAction's last parameter "more versatile". You'll have to explain that.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Simple, the app will use several GUI interfaces - in Qt terminology "widgets". There is lots of commonality and I like to reuse my code. So passing "widget" as a parameter to addAction would be nice. Besides - I need to get over this "passing function as a parameter with parameters ". I have a feeling QT makes its own syntax... Should that be function ( passFunction, passFunctionparamater ); // pointer , pointer
-
Simple, the app will use several GUI interfaces - in Qt terminology "widgets". There is lots of commonality and I like to reuse my code. So passing "widget" as a parameter to addAction would be nice. Besides - I need to get over this "passing function as a parameter with parameters ". I have a feeling QT makes its own syntax... Should that be function ( passFunction, passFunctionparamater ); // pointer , pointer
I haven't used Qt, so I don't know if I can help. It sounds like a widget is an object, but you're only passing functions and parameters. Would passing an object help? It's even possible to pass a class's member function[^] as a parameter. I've not used this capability, so you'd have to read up on it if that's what you want. The problem with passing a function and its parameters is that different functions take different parameters. To deal with that, I think you'd need std::function[^]. Again, that's not something I've used, but now you can look for more details. My go-to site for C++ is here[^]. It's a bit formal, so sometimes you need to look elsewhere for explanations and examples that are easier to understand. But it will provide you with the right terminology to use when investigating something.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.