PDA

View Full Version : Adjust QTableWidget to contents



^NyAw^
6th March 2008, 12:20
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,

jpn
6th March 2008, 12:57
See

QTableView::resizeColumnsToContents()
QTableView::resizeRowsToContents()

^NyAw^
6th March 2008, 15:02
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:


m_pqLayoutVertical = new QVBoxLayout;
m_pqLayoutVertical->addWidget(m_pqTableWidget,1);
m_pqLayoutVertical->addWidget(m_pqResetButton);

m_pqLayoutVertical2 = new QVBoxLayout;
m_pqLayoutVertical2->addWidget(m_pqPieWidget,1);
m_pqLayoutVertical2->addWidget(m_pqLabel);

m_pqLayoutHoritzontal = new QHBoxLayout;
m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical);
m_pqLayoutHoritzontal->addLayout(m_pqLayoutVertical2,1);

m_pqWidget->setLayout(m_pqLayoutHoritzontal);
m_pqDock->setWidget(m_pqWidget);

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

jpn
6th March 2008, 17:02
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.

^NyAw^
6th March 2008, 17:11
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,

jpn
6th March 2008, 17:29
Sorry, I don't remember what was it but perhaps QHeaderView::offset()?

^NyAw^
6th March 2008, 17:31
Ups,
mmm, I think this is not.

jpn
6th March 2008, 17:38
What about width of the other header then? Or QHeaderView::sectionPosition():


int logicalIndex = headerView->logicalIndex(0);
int width = headerView->sectionPosition(logicalIndex);
...

^NyAw^
6th March 2008, 18:09
Hi,

I'm doing something wrong or forgetting something.



m_pqTableWidget->setSizePolicy(QSizePolicy::Policy::Minimum,QSizePo licy::Policy::Minimum);

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

jpn
6th March 2008, 18:11
From QSizePolicy::Minimum (http://doc.trolltech.com/4.3/qsizepolicy.html#Policy-enum) 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()...

^NyAw^
6th March 2008, 18:30
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.