Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QObject iterator for QAction
Forum Updated to NodeBB v4.3 + New Features

QObject iterator for QAction

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 4 Posters 539 Views 2 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    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 QAction

    both 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;
    
    Christian EhrlicherC M 2 Replies Last reply
    0
    • A Anonymous_Banned275

      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 QAction

      both 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;
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • A Anonymous_Banned275

        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 QAction

        both 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;
        
        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #3

        @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
        }
        
        Christian EhrlicherC A 2 Replies Last reply
        0
        • M mpergand

          @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
          }
          
          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • M mpergand

            @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
            }
            
            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #5

            @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 ????

            J.HilkJ 1 Reply Last reply
            0
            • A Anonymous_Banned275

              @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 ????

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @AnneRanch
              66a864ee-e1e4-4da1-8e99-7400fd07c409-image.png


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              A 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @AnneRanch
                66a864ee-e1e4-4da1-8e99-7400fd07c409-image.png

                A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #7

                @J-Hilk arrrrrrrrrrrrr

                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