How to "trigger" from check bpx...?
-
Sorry for posting so many issues , but I like to keep each one separately.
I am making slow progress with my "menu/submenu" task.
Here is my current code:
I have have highlighted area I need help.I need help with putting correct 'trigger" code in
SIGNAL
for (int index = 0; index < list.size(); ++index) { #ifdef DEBUG_MDI text = Q_FUNC_INFO; text += " @ line "; text += QString::number(__LINE__); text += " TASK run for loop !!!!!!!!!!!!!!!"; //text += QString::number(index); qDebug() << text; #endif tempmenu = new QMenu(); tempmenu->setTitle(list[index] + " #" + QString::number(index)); subMenu[index] = m_ui->menuWindow_cpntrol; subAction[index] = subMenu[index]->addMenu(tempmenu); subAction[index]->setVisible(true); subAction[index]->setCheckable(true); qDebug() << list[index]; //list_hcitool // temporary switch here switch(index) { // switch case 0: { { //process }//process break; } case 1: { { //process for (int index = 0; index < list_hcitool.size(); ++index) { text = "Add subsubmenu"; subtempmenu = new QMenu(); subtempmenu->setTitle(list_hcitool[index] + " #" + QString::number(index)); subMenu[index] = tempmenu; //m_ui->menuWindow_cpntrol; subAction[index] = subMenu[index]->addMenu(subtempmenu); subAction[index]->setVisible(true); **subAction[index]->setCheckable(true); this adds check box to the menu TOK** subAction[index]->triggered(true); qDebug() << list_hcitool[index]; // connect here ?? // for (int index = 0; index < list.size(); ++index) { #ifdef DEBUG_MDI text = Q_FUNC_INFO; text += " @ line "; text += QString::number(__LINE__); text += " TASK run for loop !!!!!!!!!!!!!!!"; //text += QString::number(index); qDebug() << text; #endif tempmenu = new QMenu(); tempmenu->setTitle(list[index] + " #" + QString::number(index)); subMenu[index] = m_ui->menuWindow_cpntrol; subAction[index] = subMenu[index]->addMenu(tempmenu); subAction[index]->setVisible(true); subAction[index]->setCheckable(true); qDebug() << list[index]; //list_hcitool // temporary switch here switch(index) { // switch case 0: { { //process }//process break; } case 1: { { //process for (int index = 0; index < list_hcitool.size(); ++index) { text = "Add subsubmenu"; subtempmenu = new QMenu(); subtempmenu->setTitle(list_hcitool[index] + " #" + QString::number(index)); subMenu[index] = tempmenu; //m_ui->menuWindow_cpntrol; subAction[index] = subMenu[index]->addMenu(subtempmenu); subAction[index]->setVisible(true); subAction[index]->setCheckable(true); subAction[index]->triggered(true); qDebug() << list_hcitool[index]; // connect here ?? // keeps accessing the slot connect(subAction[index], **SIGNAL(hovered()), **keeps accessing the slot need "single shot" fix in SLOT ?** this,SLOT(testSlot()));** // TODO triggerd by check box //connect(subAction[index],SIGNAL(subAction[index]->triggered(true))), // this,SLOT(testSlot()); } }//process break; connect(subAction[index], SIGNAL(hovered()), SIGNAL(hovered()), this,SLOT(testSlot())); // TODO triggerd by check box connect(subAction[index],SIGNAL(subAction[index]->triggered(true))), this,SLOT(testSlot()); //connect(subAction[index],SIGNAL(subAction[index]->triggered(true))), // this,SLOT(testSlot()); } }//process break;
:
-
@AnneRanch said in How to "trigger" from check bpx...?:
connect(subAction[index],SIGNAL(subAction[index]->triggered(true))),
this,SLOT(testSlot());Dont know what exactly you are doing, but the correct connection would look like this:
connect(subAction[index], SIGNAL(triggered(bool))), this, SLOT(testSlot(bool)); // OR connect(subAction[index], &QAction::triggered, this, &WHATEVER_CLASS_YOU_ARE_IN::testSlot);
You could easily solve these kind of "issues" yourself, if you would look at the documentation.
Signals are listed here:and
triggered(bool checkstate)
is explained here: -
Could some kind person - without need to know what I am doing or without reference to RTFM kindly explain the difference between "connect"
?{ //process for (int index = 0; index < list_hcitool.size(); ++index) { text = "Add subsubmenu"; subtempmenu = new QMenu(); subtempmenu->setTitle(list_hcitool[index] + " #" + QString::number(index)); subMenu[index] = tempmenu; //m_ui->menuWindow_cpntrol; subAction[index] = subMenu[index]->addMenu(subtempmenu); subAction[index]->setVisible(true); subAction[index]->setCheckable(true); subAction[index]->triggered(true); qDebug() << list_hcitool[index]; **THIS connect runs testSlot // connect here ?? // connect(subAction[index], // SIGNAL(hovered()), // this,SLOT(testSlot()));** **THIS ONE DOES NOT connect( subAction[index], SIGNAL(triggered()), this, SLOT(testSlot())); ```** My guess - the SIGNAL "triggered" code is missing something. Thanks for your help.
-
Like @Pl45m4 mentioned, I generally recommend to use this syntax, instead of the SIGNAL / SLOT marcros:
connect(subAction[index], &QAction::triggered, this, &WHATEVER_CLASS_YOU_ARE_IN::testSlot);
The difference is: The latter uses pointer to member functions ("PMF"). It gives you a compile time error, if something doesn't work. It's much faster during runtime.
The old macro based connect syntax parses strings in meta objects, to find the right function pointer to connect to. It requires the signature of the functions to be explicitly stated. The reason why your second connect doesn't work is that the
QAction::triggered()
signal has a bool argument. You would have to specifySIGNAL(triggered(bool))
, as well as the correct signature of your test slot.
All of that isn't needed, if you use the PMF syntax.For the records: PMF syntax has some limitations with regard to default arguments, but that's corner cases. The connect happens exactly at the same time. The automatic disconnect happens later: Macro-based connects are disconnected, when the meta object goes out of scope. If you have a class inheriting from
QObject
, the disconnect happens when the inheriting class is deleted. At that time, the class is only aQObject
. PMF connections stay alive and point to a stale object. That can potentially cause crashes, when a signal gets emitted to a class in destruction. PMF connections need to be explicitly disconnected in the d'tor of the inheriting class in such cases. -
This post is deleted!
-
@Axel-Spoer
I have made some changes
and getting messages in run.l ```
{ //process
for (int index = 0; index < list_hcitool.size(); ++index)
{
text = "Add subsubmenu";
subtempmenu = new QMenu();
subtempmenu->setTitle(list_hcitool[index] + " #" + QString::number(index));
subMenu[index] = tempmenu; //m_ui->menuWindow_cpntrol;
subAction[index] = subMenu[index]->addMenu(subtempmenu);
subAction[index]->setVisible(true);
subAction[index]->setCheckable(true);
//subAction[index]->setChecked(true);
subAction[index]->triggered(true);
qDebug() << list_hcitool[index];// connect here ?? connect ( subMenu[index], SIGNAL(checked(true)), this, SLOT(testSlot(true))); connect ( subMenu[index], SIGNAL(triggered(true)), this, SLOT(testSlot(true)));
Run time messages: **( only the highlighted shoud be analysed it is a loop)** " QObject::connect: No such signal **QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590 QObject::connect: (receiver name: 'MainWindow_Bluetooth') QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595 QObject::connect: (receiver name: 'MainWindow_Bluetooth') "command hcitool dev "** QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590 QObject::connect: (receiver name: 'MainWindow_Bluetooth') QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595 QObject::connect: (receiver name: 'MainWindow_Bluetooth') "command hcitool scan " QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590 QObject::connect: (receiver name: 'MainWindow_Bluetooth') QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595 QObject::connect: (receiver name: 'MainWindow_Bluetooth') "command hcitool inq " QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590 QObject::connect: (receiver name: 'MainWindow_Bluetooth') QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595 QObject::connect: (receiver name: 'MainWindow_Bluetooth')ud nr analused, it is in loop..)
ol "
QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
"command hcitool dev "
QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
"command hcitool scan "
QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
"command hcitool inq "
QObject::connect: No such signal QMenu::checked(true) in mainwindow_Bluetooth.cpp:1590
QObject::connect: (receiver name: 'MainWindow_Bluetooth')
QObject::connect: No such signal QMenu::triggered(true) in mainwindow_Bluetooth.cpp:1595
QObject::connect: (receiver name: 'MainWindow_Bluetooth') -
@AnneRanch
As said before, if you never usedSIGNAL
/SLOT()
macros you would never get such errors.However, given that you have, you can never specify a value as a parameter to either the signal or slot as you have with the
true
inSIGNAL(checked(true))
or inSLOT(testSlot(true))
. You must always a specify the type of the value, not its value, so e.g. hereSIGNAL(checked(bool))
, and the same for the other three occurrences.However, signal QMenu::triggered(QAction *action) as you can see does not send any boolean, it sends a
QAction *
. And there is noQMenu::checked
signal at all. OTOH, void QAction::triggered(bool checked = false) does exist, and sends abool
, so maybe you mean to be connecting signals from aQAction
not aQMenu
? I still do not see anyQAction::checked
signal though, but there is a QAction::toggled(bool checked).And to be clear:
triggered
refers to clicking the menu action (i.e. do whatever it is supposed to do when clicked) whiletoggled
refers to changing the state of a checkbox you have asked the action to show (i.e. clicking the checkbox of the action rather than on the action text).@Axel-Spoerl showed a suitable
connect()
earlier:connect(subAction[index], &QAction::triggered, this, &WHATEVER_CLASS_YOU_ARE_IN::testSlot);
As you can see, it is the
QAction
signals which we connect, notQMenu
ones. -
@AnneRanch
@JonB said in How to "trigger" from check bpx...?:
However, signal QMenu::triggered(QAction *action) as you can see does not send any boolean, it sends a QAction *. And there is no QMenu::checked signal at all. OTOH, void QAction::triggered(bool checked = false) does exist, and sends a bool, so maybe you mean to be connecting signals from a QAction not a QMenu? I still do not see any QAction::checked signal though, but there is a QAction::toggled(bool checked).
That's why I wrote
@Pl45m4 said in How to "trigger" from check bpx...?:
connect(subAction[index], SIGNAL(triggered(bool))), this, SLOT(testSlot(bool));
// OR
connect(subAction[index], &QAction::triggered, this, &WHATEVER_CLASS_YOU_ARE_IN::testSlot);but I don't know why I'm doing this... You recommend something, the advice gets ignored, bullsh*t happens with tons of errors (of course) and then getting asked what's wrong again
(╯°□°)╯︵ ┻━┻I mean, you are not a new user anymore, but despite the fact that you don't have any programming "background", you should at least know how a (more or less) simple connection works.
We are going round in circles, since years.
Aren't you also eager to learn something and to finish your program?!
A lot of us are really patient with you and try the best they can to explain and "teach"... but it's pretty frustrating to deal with the same stuff over and over again. It's not wrong to ask these questions but...sometimes I just can't...
(often the solution is right there or just one click on a link / one documentation page away... If we refer, it's "RTFM"... which is not a bad thing)