Results 1 to 5 of 5

Thread: Geometry of QWebElement

  1. #1
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Geometry of QWebElement

    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:

    Qt Code:
    1. Opage = new QWebPage(this);
    2. Opage->mainFrame()->setHtml(reply->readAll());
    3. QWebElement doc = Opage->mainFrame()->documentElement();
    4. QWebElementCollection elements = doc.findAll("div");
    5. foreach (QWebElement element, elements)
    6. {
    7. QString height = element.geometry().height();
    8. qDebug() << height ;
    9. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Geometry of QWebElement

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    TeeT (17th April 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Geometry of QWebElement

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

    Qt Code:
    1. Opage = new QWebPage(this);
    2. Opage->mainFrame()->setHtml(reply->readAll());
    3. QWebElement doc = Opage->mainFrame()->documentElement();
    4. connect(Opage->mainFrame(),SIGNAL(initialLayoutCompleted()),&loop, SLOT(quit()));
    5. loop.exec();
    6. QWebElementCollection elements = doc.findAll("div");
    7. foreach (QWebElement element, elements)
    8. {
    9. QString height = element.geometry().height();
    10. qDebug() << height;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    Qt Code:
    1. "?"
    2. "?"
    3. ""?"
    4. """""r"
    5. ""A"
    6. "("
    7. """*"
    8. ""*"
    9. ""j"
    10. """"?"
    11. "U"
    12. ""
    13. "`"
    14. ""
    15. ""
    16. ""P"
    17. "?"
    18. "?"
    19. "<"
    20. """""i"
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Geometry of QWebElement

    QWebElement::geometry() returns a QRect, its height() method returns an int, so this line
    Qt Code:
    1. QString height = element.geometry().height();
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    TeeT (17th April 2015)

  7. #5
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Geometry of QWebElement

    Quote Originally Posted by anda_skoa View Post
    QWebElement::geometry() returns a QRect, its height() method returns an int, so this line
    Qt Code:
    1. QString height = element.geometry().height();
    To copy to clipboard, switch view to plain text mode 
    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!

Similar Threads

  1. how to draw on a QWebElement
    By 26_zhang@163.com in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2014, 03:25
  2. Set html <style> with QWebElement
    By poser in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2014, 18:31
  3. QWebElement render problem
    By Vaterland in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2012, 00:36
  4. QWebElement evaluateJavaScript problem
    By Talei in forum Newbie
    Replies: 0
    Last Post: 10th March 2011, 05:38
  5. Dynamically create SVG with QWebElement
    By yawar in forum Qt Programming
    Replies: 0
    Last Post: 11th April 2010, 04:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.