Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get rect of widget
QtWS25 Last Chance

Get rect of widget

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aliosa
    wrote on 27 Nov 2016, 15:10 last edited by aliosa
    #1

    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.

    K 1 Reply Last reply 27 Nov 2016, 15:13
    0
    • A aliosa
      27 Nov 2016, 15:10

      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 27 Nov 2016, 15:13 last edited by
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        aliosa
        wrote on 27 Nov 2016, 15:19 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aliosa
          wrote on 27 Nov 2016, 15:32 last edited by
          #4

          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();
          

          }

          K 1 Reply Last reply 27 Nov 2016, 15:38
          0
          • A aliosa
            27 Nov 2016, 15:32

            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();
            

            }

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 27 Nov 2016, 15:38 last edited by
            #5

            This is what I meant, yes.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0

            2/5

            27 Nov 2016, 15:13

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved