Results 1 to 11 of 11

Thread: Adjust QTableWidget to contents

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Adjust QTableWidget to contents

    Hi,

    How can I adjust the QTableWidget to the contents? I want it to resize to show all columns and rows that are in the table.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adjust QTableWidget to contents

    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjust QTableWidget to contents

    Hi,

    This fuctions allow me to adjust the row and columns, but what I need is to adjust the size of the QTableWidget to the columns. The QTableWidget is on a Layout with other elements.

    This is the code:
    Qt Code:
    1. m_pqLayoutVertical = new QVBoxLayout;
    2. m_pqLayoutVertical->addWidget(m_pqTableWidget,1);
    3. m_pqLayoutVertical->addWidget(m_pqResetButton);
    4.  
    5. m_pqLayoutVertical2 = new QVBoxLayout;
    6. m_pqLayoutVertical2->addWidget(m_pqPieWidget,1);
    7. m_pqLayoutVertical2->addWidget(m_pqLabel);
    8.  
    9. m_pqLayoutHoritzontal = new QHBoxLayout;
    10. m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical);
    11. m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical2,1);
    12.  
    13. m_pqWidget->setLayout(m_pqLayoutHoritzontal);
    14. m_pqDock->setWidget(m_pqWidget);
    To copy to clipboard, switch view to plain text mode 
    So, what I want is a table that is resized to the columns(there are always 3 columns and the first one added when adding a row).
    Here is what I don't want
    Attached Images Attached Images
    Òscar Llarch i Galán

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adjust QTableWidget to contents

    Then look at QTableView::horizontalHeader() and QTableView::verticalHeader(). QHeaderView provides all needed information for calculations. You might also want to search the forums. I believe this has been asked a few times before.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjust QTableWidget to contents

    Hi,

    Thanks jpn,

    I have been reading the docs that you told me and using them. I'm calculating the space of the columns and setting the sum as the size of the table and the table sizePolicy to minimum.
    The only column that I'm not able to get the width is the column added when adding a row(the top-left column).

    Thanks again,
    Òscar Llarch i Galán

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adjust QTableWidget to contents

    Sorry, I don't remember what was it but perhaps QHeaderView::offset()?
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjust QTableWidget to contents

    Ups,
    mmm, I think this is not.
    Òscar Llarch i Galán

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adjust QTableWidget to contents

    What about width of the other header then? Or QHeaderView::sectionPosition():
    Qt Code:
    1. int logicalIndex = headerView->logicalIndex(0);
    2. int width = headerView->sectionPosition(logicalIndex);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjust QTableWidget to contents

    Hi,

    I'm doing something wrong or forgetting something.

    Qt Code:
    1. m_pqTableWidget->setSizePolicy(QSizePolicy::Policy::Minimum,QSizePolicy::Policy::Minimum);
    2.  
    3. //Width of the table
    4. int iWidth = 0;
    5. for (int i=0; i<m_pqTableWidget->columnCount(); i++)
    6. {
    7. iWidth += m_pqTableWidget->horizontalHeader()->sectionSize(i);
    8. }
    9. iWidth += m_pqTableWidget->verticalHeader()->width();
    10. m_pqTableWidget->setMinimumWidth(iWidth);
    11.  
    12. //Height of the table
    13. int iHeight = 0;
    14. for (int i=0; i<m_pqTableWidget->rowCount(); i++)
    15. {
    16. iHeight += m_pqTableWidget->verticalHeader()->sectionSize(i);
    17. }
    18. iHeight += m_pqTableWidget->horizontalHeader()->height();
    19. m_pqTableWidget->setMaximumHeight(iHeight);
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adjust QTableWidget to contents

    From QSizePolicy::Minimum docs:
    The sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().
    You should reimplement QTableWidget::sizeHint()...
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adjust QTableWidget to contents

    Hi,

    What I need is that the table sets its minimum width to the contents, and the maximum height to the contents. So the tableWidget can be resized horizontally(but never less that minimum size) and can't be resized vertically.

    The code is going so well now but "width" and "height" are not returning me the desired values.
    Òscar Llarch i Galán

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2007, 00:29

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.