QObject iterator for QAction
-
I am posting this WORK IN Progress code hoping somebody will help me to understand and / or evaluate / improve the concept.
The purpose of code is to execute specific action, such action code is in another object , not in current object. These objects are in C++ class hierarchy structure , sort of tree.The QList and "foreach" loops works on QObject and in general do the required task.
The problem is when "textMmatch" is found.
Since the iteration and list is done n QObject - the match is therefore QObject.I need to find the actual QAction - which is part of the hierarchy.
To illustrate
I have "actionTile_subwindiws" QObject and
associated
on_actionTile_subwindoiws QActionboth of these values were build by Qt
I cannot simply cast QObject to QAction , I need to do more
scan to find the QObject and its corresponding QAction.I am asking for code or conceptual help
how and where in existing code to add this additional scan.I could post the entire debug tree now , but it is little long...
I will wait until I get repose and / or request for it.Thanks
// Accept the event to allow the window to close event->accept(); // TOK // looking for action = tileSubwindows QString textMmatch = "actionTile_subwindows"; //QString textMmatch = "on_actionTile"; // test code here int matchFound = 0; QAction *pAction; QObject *pItemObject; QObject* p=this; #ifdef ITERATE text = " base class "; text += p->objectName(); qDebug().noquote() << text; #endif while(p->parent()) { // next parent p = p->parent(); #ifdef ITERATE text = " grand parent (up) class "; text += p->objectName(); qDebug().noquote() << text; #endif if(p) { #ifdef ITERATE text = " grand parent (up) children classes "; text += p->objectName(); qDebug().noquote() << text; #endif QObjectList pListChildren = p->children(); // scan for QObject foreach(auto *itemChild,pListChildren) { if( itemChild->objectName().contains(textMmatch)) { #ifdef ITERATE text = " \t\t\t *** found match "; text += itemChild->objectName(); qDebug().noquote() << text; //break; //continue; #endif '**ADD QAction scan here ??** pItemObject = itemChild; matchFound++; break; //continue; } else { text = " child (object) not found continue search... "; } #ifdef ITERATE text += itemChild->objectName(); qDebug().noquote() << text; #endif } if(matchFound) break; #ifdef ITERATE text += Q_FUNC_INFO; text += QString::number(__LINE__); qDebug().noquote() << text; #endif } } if(!matchFound) { text = " FAILED no match found "; qDebug().noquote() << text; } #ifdef ITERATE text = " DONE "; text += " action object name "; text += pItemObject->objectName(); qDebug().noquote() << text; //break; //continue; #endif //THIS IS NO GOOD QAction *action = qobject_cast<QAction *>(pItemObject); #ifdef ITERATE text = " actinon name "; text += action->objectName(); qDebug().noquote() << text; #endif action->trigger(); return;
-
@AnneRanch said in QObject iterator for QAction:
I cannot simply cast QObject to QAction
Why not? You found the correct qobject so cast it as explained here: https://forum.qt.io/topic/156638/qobject-or-qaction/2
-
@AnneRanch said in QObject iterator for QAction:
QAction *action = qobject_cast<QAction *>(pItemObject);
pItemObject is not a QAction
Instead, search through QActions in the main window (top level widget)QWidget* p=this; while(p->parentWidget()) p = p->parentWidget(); // p need to be a QWidget, hence parentWidget() QList<QAction*> actions=p->actions(); for(QAction* action : actions) { // test for your action here }
-
@mpergand said in QObject iterator for QAction:
pItemObject is not a QAction
According to https://forum.qt.io/topic/156638/qobject-or-qaction it is.
-
@mpergand Thanks , so I was try to "fix" the result - QObject- and it shlud be fixed in the beginning....
You are the MAN!
I owe you so much for your help.This "improved (?) forum format " needs another way to let people know who is the real guru here.... and I do not mean rating the post, that is for bean counters and sucks.
I would suggest that , but nobody would pay attention to MY suggestions so
is here somebody willing to suggest that ???? -
@AnneRanch