PDA

View Full Version : How to use "Foreach" with "Qaxobject"?



DJW602
8th August 2016, 11:41
I want to pick subelements from a Qaxobject,but it is failed with Vs2015:


QString x = "";
QAxObject* MyFrames = MyWebObj->querySubObject("Document")->querySubObject("Frames");

foreach(QAxObject* MyFrame, MyFrames)//somethingwrong here....
{
x += MyFrame->querySubObject("Document")->querySubObject("All")
->querySubObject("Item(Int32)", 0)->dynamicCall("OuterHtml()").toString();
}
QAxObject* MyAll=MyWebObj->querySubObject("Document")->querySubObject("All")->querySubObject("Item(Int32)", 0);
x= MyAll->dynamicCall("OuterHtml()").toString();//workable
qDebug() << x;
return x;


so I have to change it with for loop:


QString x = "";
QAxObject* MyFrames = MyWebObj->querySubObject("Document")->querySubObject("Frames");

QAxObject* MyFrame = 0;
x = MyWebObj->querySubObject("Document")->querySubObject("All")
->querySubObject("Item(Int32)", 0)->dynamicCall("OuterHtml()").toString();
for(int i=0;;i++)
{
MyFrame = MyFrames->querySubObject("Item(Int32)", i);
if (!MyFrame) break;
x += MyFrame->querySubObject("Document")->querySubObject("All")
->querySubObject("Item(Int32)", 0)->dynamicCall("OuterHtml()").toString();
}
qDebug() << x;
return x;


My question is how to use foreach with Qaxobject, as it looks more simple.

anda_skoa
8th August 2016, 12:01
foreach, like the C++11 range-for, works on a container, i.e. an object of a class that provides STL-style iterators.

QAxObject does not have those.

You could use assignment-in-condition if you want to have something like


for (int i = 0; (MyFrame = MyFrames->querySubObject("Item(Int32)", i)) != 0; ++i) {
// ....
}


Cheers,
_