Get rect of widget



  • Hello

    The question is

    How and when to get the rect of widget ?

    I know the function rect but...

    I have a desktop app and I created dinamicaly the widgets ( one QTabWidget with 3 tabs
    and each tab contains few QGroupBoxes with different layouts and some widgets inside ).
    I set the size and the postion of main screen at the beginning before call show();

    I want to know the rect of one widget from one groupbox from one tab after the app is created and showed
    in order to draw something in that widget.

    I do not know when and where to get the rect of that widget.
    When I created that widget the rect is not correct.

    This is code for one groupbox created for one tab

    void CalculTab::createGroupBoxGraphic2()
    {
    m_pGroupG2 = new QGroupBox(tr("G2"));

    QHBoxLayout *layout = new QHBoxLayout;
    

    // this is my widget
    GLWidget *openGL = new GLWidget(&m_graphic2, this);

    layout->addWidget(openGL);
    
    m_pGroupG2->setLayout(layout);
    
    // the value is not correct !!!
    QRect rcG2 = openGL->rect();
    

    }

    For test I added mousePressEvent for GLWidget .
    After the app is created and showed, when I click I can get the correct recatngle.

    void GLWidget::mousePressEvent(QMouseEvent *e)
    {
    // correct
    QRect rcG2 = this->rect();

    }

    I hope you can understand what I want.


  • Qt Champions 2016

    @aliosa said in Get rect of widget:

    I want to know the rect of one widget from one groupbox from one tab after the app is created and showed
    in order to draw something in that widget.

    Just draw in the paintEvent override, don't try to guess when the widget is shown or hidden or what the intermediate heights are, it's not going to work well.



  • Hello

    I need to know that rect because I want to pass to other class and make some calculation.

    Thank you for your answer but it is not what I need.

    I want to know when and where I can obtain the correct rectangle of one widget.
    Maybe it is an event which can tell me that app is created and all the size and position of controls are ready.



  • Maybe in this function GLWidget::paintEvent(QPaintEvent *event)

    GLWidget is my widget and I pass the rect to other object m_graphic which will do some.

    But I am not sure that is the best solution.

    void GLWidget::paintEvent(QPaintEvent *event)
    {
    QRect rect = event->rect();

    m_graphic->SetRecatangleGraphic(rect);
    
    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing);
    m_graphic->paint(&painter, event);
    painter.end();
    

    }


  • Qt Champions 2016

    This is what I meant, yes.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.