Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. foreach....recursively - assistance with C++ code wnated

foreach....recursively - assistance with C++ code wnated

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasedata-structuresregexhelp
6 Posts 2 Posters 28 Views 1 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am asking for code assistance, I am NOT asking for opinions, to implement "foreach" to be able to write something likes "action-> trigger this code " I am asking for code help to rewrite this to recursively go thru the object hierarchy until "textMatch" is found

    QString textMatch = test; // "tile";
    QList pTEST = parent->findChildren();

        foreach( auto \*action, pTEST)
        {
            text = action->objectName();
            qDebug().noquote() << text;
            if(text.contains(textMatch))
            {
    

    #ifdef RETILE
    text = "match found parent ";
    text += parent->objectName();
    text += "\n action ";
    text += action->objectName();
    qDebug().noquote() << text;
    #endif

                action->trigger();
                text += "  foreach match ";
                qDebug().noquote() << text;
                return 0;
                break;
            }
        }
    

    The code abiove works fine "in single stage", I need it to iterate , using foreach preferred, but not mandatory, thru the entire tree. PLEASE NOTE This is an "under construction code " and will be deleted AFTER solution is found.

    "actionTile_subwindows" QAction

    Locals		
    	event	@0x7fffffffca20	QCloseEvent
    	mdiParent	@0x7ffff4ea5453	QMdiSubWindow
    	pActions		QList
    	pL		QList
    	pLParent		QList
    	pLParent\_Action		QList
    	pOBJECT	0x5d0000006e	QWidget\*
    	pTEST	@0x7fffffffc550	QWidget
    	this	"SettingsDialog"	SettingsDialog
    		\[QDialog\]	"SettingsDialog"	QDialog
    		\[d\]	@0x555555d923e0	QDialogPrivate
    		\[parent\]	@0x555555de3310	QMdiSubWindow
    			\[QWidget\]	@0x555555de3310	QWidget
    			\[d\]	@0x5555556d1000	QMdiSubWindowPrivate
    			\[parent\]	@0x555555c30e80	QWidget
    				\[QObject\]	@0x555555c30e80	QObject
    				\[QPaintDevice\]	@0x555555c30e90	QPaintDevice
    				\[d\]	@0x555555c30ec0	QWidgetPrivate
    				\[parent\]	@0x555555c64250	QMdiArea
    					\[QAbstractScrollArea\]	@0x555555c64250	QAbstractScrollArea
    					\[d\]	@0x5555559ff1a0	QMdiAreaPrivate
    					\[parent\]	"MainWindow\_Bluetooth"	MainWindow\_Bluetooth
    						\[QMainWindow\]	"MainWindow\_Bluetooth"	QMainWindow
    						\[d\]	@0x555555c2ed00	QMainWindowPrivate
    						\[parent\]	@0x555555ccf290	QMdiSubWindow
    						\[children\]	<35 items>	QList
    							\[0\]	"\_layout"	QMainWindowLayout
    
    K 1 Reply Last reply
    0
    • L Lost User

      I am asking for code assistance, I am NOT asking for opinions, to implement "foreach" to be able to write something likes "action-> trigger this code " I am asking for code help to rewrite this to recursively go thru the object hierarchy until "textMatch" is found

      QString textMatch = test; // "tile";
      QList pTEST = parent->findChildren();

          foreach( auto \*action, pTEST)
          {
              text = action->objectName();
              qDebug().noquote() << text;
              if(text.contains(textMatch))
              {
      

      #ifdef RETILE
      text = "match found parent ";
      text += parent->objectName();
      text += "\n action ";
      text += action->objectName();
      qDebug().noquote() << text;
      #endif

                  action->trigger();
                  text += "  foreach match ";
                  qDebug().noquote() << text;
                  return 0;
                  break;
              }
          }
      

      The code abiove works fine "in single stage", I need it to iterate , using foreach preferred, but not mandatory, thru the entire tree. PLEASE NOTE This is an "under construction code " and will be deleted AFTER solution is found.

      "actionTile_subwindows" QAction

      Locals		
      	event	@0x7fffffffca20	QCloseEvent
      	mdiParent	@0x7ffff4ea5453	QMdiSubWindow
      	pActions		QList
      	pL		QList
      	pLParent		QList
      	pLParent\_Action		QList
      	pOBJECT	0x5d0000006e	QWidget\*
      	pTEST	@0x7fffffffc550	QWidget
      	this	"SettingsDialog"	SettingsDialog
      		\[QDialog\]	"SettingsDialog"	QDialog
      		\[d\]	@0x555555d923e0	QDialogPrivate
      		\[parent\]	@0x555555de3310	QMdiSubWindow
      			\[QWidget\]	@0x555555de3310	QWidget
      			\[d\]	@0x5555556d1000	QMdiSubWindowPrivate
      			\[parent\]	@0x555555c30e80	QWidget
      				\[QObject\]	@0x555555c30e80	QObject
      				\[QPaintDevice\]	@0x555555c30e90	QPaintDevice
      				\[d\]	@0x555555c30ec0	QWidgetPrivate
      				\[parent\]	@0x555555c64250	QMdiArea
      					\[QAbstractScrollArea\]	@0x555555c64250	QAbstractScrollArea
      					\[d\]	@0x5555559ff1a0	QMdiAreaPrivate
      					\[parent\]	"MainWindow\_Bluetooth"	MainWindow\_Bluetooth
      						\[QMainWindow\]	"MainWindow\_Bluetooth"	QMainWindow
      						\[d\]	@0x555555c2ed00	QMainWindowPrivate
      						\[parent\]	@0x555555ccf290	QMdiSubWindow
      						\[children\]	<35 items>	QList
      							\[0\]	"\_layout"	QMainWindowLayout
      
      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      There's probably a nice template way to do this. But if I understand you're problem, here's a simple C++ program with multi-level tree that you may be able to adapt to your situation:

      #include
      #include

      class Tree {
      public:
      int datum;
      std::vector children;
      Tree(int d) :datum(d) {}
      };

      void walk(Tree& t, size_t n, void fn(const Tree& t, size_t n) )
      {
      fn(t, n);
      for(auto child: t.children)
      walk(child, n+1, fn);
      }

      int main()
      {
      Tree t(0);
      for(int x = 1; x < 3; ++x)
      {
      Tree tmp(x);
      t.children.push_back(tmp);
      }
      for(int y = 100; y < 500; y += 100 )
      {
      Tree tmp(y);
      t.children[0].children.push_back(tmp);
      }

      walk(t, 0,  \[\](const Tree& t, size\_t indent) { std::cout << std::string(indent\*3, ' ') << t.datum << '\\n'; });
      

      }

      This processes the parent before the children, but you can reverse the lines of the walk function to do the children first, if preferred. foreach is not part of the C++ standard, but the ranged for loop has been since C++-11, so your compiler almost certainly supports it. Whether QT containers will work with ranged for loops is not known to me. Still, the idea is the same. Create a recursive function that processes the parent, then calls itself on each of the children. I'm sure there's a more generic way to do this using templates, that parameterizes the function, which would mean you don't have to write a separate walk function for every distinct function prototype on fn. Maybe that's an exercise you could undertake to help improve your understanding of templates, and other C++ facilities.

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      L 1 Reply Last reply
      0
      • K k5054

        There's probably a nice template way to do this. But if I understand you're problem, here's a simple C++ program with multi-level tree that you may be able to adapt to your situation:

        #include
        #include

        class Tree {
        public:
        int datum;
        std::vector children;
        Tree(int d) :datum(d) {}
        };

        void walk(Tree& t, size_t n, void fn(const Tree& t, size_t n) )
        {
        fn(t, n);
        for(auto child: t.children)
        walk(child, n+1, fn);
        }

        int main()
        {
        Tree t(0);
        for(int x = 1; x < 3; ++x)
        {
        Tree tmp(x);
        t.children.push_back(tmp);
        }
        for(int y = 100; y < 500; y += 100 )
        {
        Tree tmp(y);
        t.children[0].children.push_back(tmp);
        }

        walk(t, 0,  \[\](const Tree& t, size\_t indent) { std::cout << std::string(indent\*3, ' ') << t.datum << '\\n'; });
        

        }

        This processes the parent before the children, but you can reverse the lines of the walk function to do the children first, if preferred. foreach is not part of the C++ standard, but the ranged for loop has been since C++-11, so your compiler almost certainly supports it. Whether QT containers will work with ranged for loops is not known to me. Still, the idea is the same. Create a recursive function that processes the parent, then calls itself on each of the children. I'm sure there's a more generic way to do this using templates, that parameterizes the function, which would mean you don't have to write a separate walk function for every distinct function prototype on fn. Maybe that's an exercise you could undertake to help improve your understanding of templates, and other C++ facilities.

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thank you. I was hoping to build on my current knowledge and working code , regardless if it is standard or not. I am looking into this

        QTreeWidgetItemIterator it(parent(this));

        and have two issues with it I still cannot access the object "parent" and it looks as this class expects tree widget to iterate. Moore RTFM.

        K 1 Reply Last reply
        0
        • L Lost User

          Thank you. I was hoping to build on my current knowledge and working code , regardless if it is standard or not. I am looking into this

          QTreeWidgetItemIterator it(parent(this));

          and have two issues with it I still cannot access the object "parent" and it looks as this class expects tree widget to iterate. Moore RTFM.

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          Salvatore Terress wrote:

          Moore RTFM

          Correct

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          L 2 Replies Last reply
          0
          • K k5054

            Salvatore Terress wrote:

            Moore RTFM

            Correct

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It worked... I did save you a time to write FOUR letters... Maybe I should end my post instead of Cheers ( I hope you take this post as a joke ...good night from Huston )

            1 Reply Last reply
            0
            • K k5054

              Salvatore Terress wrote:

              Moore RTFM

              Correct

              "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well, I am getting few "try it this way". I believe there are many ways to "skin a cat" , but these well meaning alternative suggestion are distraction from the task at hand. Actually I have to "back-paddle " in my "foreach"' approach. Before I can iterate I have to have a list and it better be of compatible type... That also applies if I use any of the Qt ways to iterate... Cheers

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups