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. [SOLVED] Widget size after being added to a layout.
QtWS25 Last Chance

[SOLVED] Widget size after being added to a layout.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.7k 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.
  • C Offline
    C Offline
    Cayan
    wrote on last edited by
    #1

    It seems like the width of a widget does not get updated after it joins a layout.

    First example:
    @
    setFixedSize(400, 400);
    m_table = new QTableWidget(this);
    m_table->setGeometry(0, 0, 50, 50);
    m_table->setRowCount(10);
    qDebug() << m_table->width();

    m_box = new QWidget(this);
    m_box->setFixedSize(200, 200);

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_table);
    qDebug() << m_table->width();

    m_box->setLayout(layout);
    qDebug() << m_table->width();
    @

    First output: http://img109.imageshack.us/img109/1310/86203198.png

    Second example:
    @
    setFixedSize(400, 400);
    m_table = new QTableWidget(this);
    m_table->setGeometry(0, 0, 50, 50);
    m_table->setRowCount(10);
    qDebug() << m_table->width();
    @

    Second output: http://img690.imageshack.us/img690/9954/84822141.png

    Why do I need it?
    I usually adjust the columns width of my tables like this:
    @
    m_table->setColumnWidth(0, m_table->width()*0.8);
    m_table->setColumnWidth(1, m_table->width()*0.2);
    @

    Since I started using layouts for tableWidgets I can't manage properly the columns width, is there another solution?

    Thanks in advance,
    Cayan.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      Go through all your rows and for each one setColumnWidth(int colNr, int width);

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Santosh Reddy
        wrote on last edited by
        #3

        Adding tableWidgts will force the table to follow the layout restriction, which may be too small for the columns to fit in such case scrolling is the only option.

        Try this.

        • Turn off scrolling in the table.
        • Add the table to scroll area
        • Add the scroll area to a layout.

        SS

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cayan
          wrote on last edited by
          #4

          @b1gsnak3, like I said I usually do it, check the last code.

          @Santosh Reddy, the issue lies on the not updated widget's width. I don't want a fix width for the columns, I'm looking for a relative size. Doing so, there is no need for scrolls.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            the "problem" is the following:
            You are checking the width in the constructor, even before the widget is shown for the first time not layouted once, thus the values are always the same as you've set them at the beginning.
            Do your column size adjustments in the resize event. For example you can use an event filter for that purpose:
            @
            m_table->installEventFilter(this);
            ....

            bool MyClass::eventFilter(QObject* watched, QEvent* event)
            {
            if( watched == m_table && event->type() == QEvent::Resize )
            {
            QResizeEvent* re = static_cast<QResizeEvent*>(event);
            m_table->setColumnWidth(0, re->size().width()*0.8);
            m_table->setColumnWidth(1, re->size().width()*0.2);
            }
            return false;
            }
            @

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Cayan
              wrote on last edited by
              #6

              @raven-worx, your solution worked. Thanks!

              1 Reply Last reply
              0

              • Login

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