Results 1 to 6 of 6

Thread: Can't get geometry() in form constructor

  1. #1
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Can't get geometry() in form constructor

    I've QGridLayout on my widget and I need to add several elements to it during runtime.
    Everything is fine, but I need to calculate and store proper height for added elements. I've tried to get it through
    Qt Code:
    1. ui->setupUi(this);
    2. ...
    3. createRuler(minValue, maxValue, steps);
    To copy to clipboard, switch view to plain text mode 
    in createRuler I've tried this :
    Qt Code:
    1. int frame_height = ui->rulerLayout->geometry().height() / steps;
    To copy to clipboard, switch view to plain text mode 
    But it allways return 0; However inside setupUi there is a code :
    Qt Code:
    1. gridLayoutWidget = new QWidget(rulerFrame);
    2. gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
    3. gridLayoutWidget->setGeometry(QRect(0, 0, 61, 361));
    4. rulerLayout = new QGridLayout(gridLayoutWidget);
    5. rulerLayout->setSpacing(6);
    6. rulerLayout->setMargin(11);
    7. rulerLayout->setObjectName(QString::fromUtf8("rulerLayout"));
    8. rulerLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    9. rulerLayout->setContentsMargins(0, 0, 0, 0);
    10.  
    11. horizontalLayout->addWidget(rulerFrame);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can't get geometry() in form constructor

    The geometry gets calculated only when the widget gets visible, and in the constructor, it is not yet visible.
    One way to over come this, is to initialize your children in the showEvent() handler.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following 2 users say thank you to high_flyer for this useful post:

    kartun (28th January 2011), Stef (28th January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can't get geometry() in form constructor

    Tried this :
    Qt Code:
    1. void Gauge::showEvent(QShowEvent *e)
    2. {
    3. QWidget::showEvent( e );
    4. createRuler(_lower_bound, _upper_bound, _steps);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Tnx for solution.

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can't get geometry() in form constructor

    Note, that you code will create the ruler EVERY time your widget gets a show event, and that can happen when you resize, or when it was under another windows that is moved etc.
    You might want to add a condition logic to when it should do this - and not for every show event.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Can't get geometry() in form constructor

    I've got feeling that you should react on QWidget::resizeEvent and there do the calculations.

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

    Default Re: Can't get geometry() in form constructor

    resizeEvent for the ruler, of course...
    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.


Similar Threads

  1. Geometry shader with QT4.7
    By saraksh in forum General Programming
    Replies: 3
    Last Post: 28th March 2011, 17:40
  2. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 19:19
  3. QGraphicsLayout::geometry()
    By isutruk in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 16:43
  4. Hiding a form and opening another form
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 20th February 2008, 16:04
  5. Constructor of form in QTDesgner ?
    By safknw in forum Qt Tools
    Replies: 1
    Last Post: 24th August 2006, 14:53

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.