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,
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
J-P Nurmi
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:
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).Qt Code:
m_pqLayoutVertical->addWidget(m_pqTableWidget,1); m_pqLayoutVertical->addWidget(m_pqResetButton); m_pqLayoutVertical2->addWidget(m_pqPieWidget,1); m_pqLayoutVertical2->addWidget(m_pqLabel); m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical); m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical2,1); m_pqWidget->setLayout(m_pqLayoutHoritzontal); m_pqDock->setWidget(m_pqWidget);To copy to clipboard, switch view to plain text mode
Here is what I don't want
Òscar Llarch i Galán
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
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
Sorry, I don't remember what was it but perhaps QHeaderView::offset()?
J-P Nurmi
Ups,
mmm, I think this is not.
Òscar Llarch i Galán
What about width of the other header then? Or QHeaderView::sectionPosition():
Qt Code:
int logicalIndex = headerView->logicalIndex(0); int width = headerView->sectionPosition(logicalIndex); ...To copy to clipboard, switch view to plain text mode
J-P Nurmi
Hi,
I'm doing something wrong or forgetting something.
Qt Code:
//Width of the table int iWidth = 0; for (int i=0; i<m_pqTableWidget->columnCount(); i++) { iWidth += m_pqTableWidget->horizontalHeader()->sectionSize(i); } iWidth += m_pqTableWidget->verticalHeader()->width(); m_pqTableWidget->setMinimumWidth(iWidth); //Height of the table int iHeight = 0; for (int i=0; i<m_pqTableWidget->rowCount(); i++) { iHeight += m_pqTableWidget->verticalHeader()->sectionSize(i); } iHeight += m_pqTableWidget->horizontalHeader()->height(); m_pqTableWidget->setMaximumHeight(iHeight);To copy to clipboard, switch view to plain text mode
Òscar Llarch i Galán
From QSizePolicy::Minimum docs:
You should reimplement QTableWidget::sizeHint()...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().
J-P Nurmi
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
Bookmarks