foreach....recursively - assistance with C++ code wnated
-
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;
#endifaction->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
-
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;
#endifaction->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
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
#includeclass 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 separatewalk
function for every distinct function prototype onfn
. 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
-
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
#includeclass 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 separatewalk
function for every distinct function prototype onfn
. 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
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.
-
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.
-
Salvatore Terress wrote:
Moore RTFM
Correct
"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
Salvatore Terress wrote:
Moore RTFM
Correct
"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
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