PDA

View Full Version : Geometry of QWebElement



TeeT
16th April 2015, 22:12
Hello Everyone,

Super QT newbie here. I was hoping someone could help with a problem I'm having. I'm looking for an example of how to get the geometry from a QWebElement. I've been looking and haven't seen any examples to find the geometry. Currently my code looks like this:



Opage = new QWebPage(this);
Opage->mainFrame()->setHtml(reply->readAll());
QWebElement doc = Opage->mainFrame()->documentElement();
QWebElementCollection elements = doc.findAll("div");
foreach (QWebElement element, elements)
{
QString height = element.geometry().height();
qDebug() << height ;
}


This prints out nothing. I also tried another case setting my element = doc.firstChild() but that also prints nothing. I am sure that I'm connecting and I have found attributes using doc.findall("div"). Do I need to use QWebView to see the dimensions? Or might I be over looking something simple?

Thanks in advance!

wysota
16th April 2015, 22:23
I would assume the page didn't have time to layout itself yet so height of the element was not calculated yet. I would wait for the QWebFrame::initialLayoutCompleted() signal before checking geometry.

TeeT
16th April 2015, 23:25
Thank you for the quick reply!

Alright so I added a QEventloop to make sure that it reaches initialLayoutCompleted(). I'm still new to siganls and slots but I believe this is right. It does print something now! :D However it doesn't make any sense. Its some random characters, but it should return an int. Also "int height" instead of "QString height" always returns a 0. Any suggestions?



Opage = new QWebPage(this);
Opage->mainFrame()->setHtml(reply->readAll());
QWebElement doc = Opage->mainFrame()->documentElement();
QEventLoop loop;
connect(Opage->mainFrame(),SIGNAL(initialLayoutCompleted()),&loop, SLOT(quit()));
loop.exec();
QWebElementCollection elements = doc.findAll("div");
foreach (QWebElement element, elements)
{
QString height = element.geometry().height();
qDebug() << height;
}


Output:
"?"
"?"
""?"
"""""r"
""A"
"("
"""*"
""*"
""j"
""""?"
"U"
""
"`"
""
""
""P"
"?"
"?"
"<"
"""""i"

anda_skoa
17th April 2015, 07:04
QWebElement::geometry() returns a QRect, its height() method returns an int, so this line


QString height = element.geometry().height();

is clearly wrong.

If you want a numerical value in a string use a formatting function, e.g. QString::number(), but why do you want a size value as a string?

Cheers,
_

TeeT
17th April 2015, 15:48
QWebElement::geometry() returns a QRect, its height() method returns an int, so this line


QString height = element.geometry().height();

is clearly wrong.

If you want a numerical value in a string use a formatting function, e.g. QString::number(), but why do you want a size value as a string?

Cheers,
_

Yep that did it! I feel a bit stupid now lol, I'm not sure what I was thinking :/

Thank you anda_skoa and wysota for your help!