Important: Please read the Qt Code of Conduct -

Getting widget size and position

  • Qt and Linux novice, Qt version 3. I cannot copy paste from my work computer to here so please try to accommodate typos.
    The question:
    What code is used to get the size of a widget and its location on the screen?
    Getting the size and position of the first widget is proving more difficult than expected so I created a new project with just one widget and some fields that show the noted values. The widget constructor sets the min width set to 666 and min height set to 222. It opens in the top left corner of the monitor leading to the presumption that the location is X and Y = zero. Here are the snippets:

    QString text;
    text = QString( %1".arg( x( ) );
    m_le_x_pos->setText( text );  // a line edit box to display the values
    text = QString( %1".arg( y( ) );
    m_le_y_pos->setText( text );

    The values returned are 800 and 360. I cannot make any sense of those two numbers. The left and top are zero and the right and bottom should be 666 and 222.

    QRect fg = frameGeometry()
    text = QString( %1".arg( ) );
    m_le_top->setText( text );    // produces the value 360
    text = QString( %1".arg( fg.bottom( ) );
    m_le_bottom->setText( text );    // produces the value 839

    I am unable to determine the meanings of those values.

  • Lifetime Qt Champion

    Is you widget in a layout or something like this? When do you retrieve the values? You must not read them when the widget is not yet shown (which is the case e.g. in the ctor)

  • The values were being fetched from within the constructor. Moving the query code out, into a function, and adding a pushbutton to call the function made the difference. The widget can be moved around and the button clicked to see how the values change. Now I can make sense of the numbers.
    Thank you for your time.

Log in to reply