PDA

View Full Version : QToolBox Resizing to contents??



kerwim
27th June 2008, 14:27
Hi,

Operating System: Ubuntu, Qt Version 4.3

I've got problems resizing a QToolBox to its contents. I created a QToolBox and added to each tab a QWidget, each has a different height (width is the same). At the beginning every tab of the QToolBox adjusts to the size of the Widget and displays it correctly (without scrollbar, space...). One QWidget contains a QTableWidget, which is dynamic, that means I can choose if just one row is displayed or two, your three... (by pressing a QPushbutton) At the beginning there are 8 rows. If pressing the button, a signal is emitted and the method updateSegments() should now adjust the size of the QToolBox Tab to the new size of the QTableWidget. Unfortunately this doesn't happen.
I wonder if I have the wrong QSizePolicy, etc.. I tried with adjustSize(), resize()... in updateSegments().
Has anybody a clue how to solve the problem?

Thank you!!!

I attach part of the code:


initializeControlBox()
{
controlBox = new QGroupBox();
controlBox->setFixedWidth(325);

m_pTabControl = new QToolBox(controlBox);

m_pArmconfig = new QWidget();
m_pTabControl->addItem(m_pArmconfig, tr("Arm Configuration"));

m_pToolLayoutVertical = new QVBoxLayout();
m_pToolLayoutVertical->addWidget(m_pTabControl);
m_pToolLayoutVertical->setAlignment(m_pTabControl, Qt::AlignTop);
controlBox->setLayout(m_pToolLayoutVertical);

//Widget Arm Configuration
m_pSegLayout = new QGridLayout();

m_pNumberSeg = new QLabel("Number of segments");
m_pSelectSeg = new QSpinBox();
m_pSelectSeg->setMaximum(8);
m_pSelectSeg->setMinimum(3);
m_pSelectSeg->setValue(8);
m_pApplySeg = new QPushButton("Apply Segments");
m_pApplySeg->setFixedWidth(110);

m_pSelectSegLayout = new QGridLayout();
m_pSelectSegLayout->addWidget(m_pNumberSeg, 0, 0);
m_pSelectSegLayout->addWidget(m_pSelectSeg, 0, 1);
m_pSelectSegLayout->setVerticalSpacing(5);
m_pSelectSegLayout->addWidget(m_pApplySeg, 1, 1);
m_pSelectSegLayout->setAlignment(m_pApplySeg, Qt::AlignRight);

// QTableWidget
m_pSegTable = new QTableWidget();
m_pSegTable->setRowCount(8);
m_pSegTable->setColumnCount(4);

vboxConfig = new QVBoxLayout();
vboxConfig->addItem(m_pSelectSegLayout);
vboxConfig->addSpacing(10);
vboxConfig->addWidget(m_pSegTable,0, 0);
m_pArmconfig->setLayout(vboxConfig);
}

updateSegments() {
m_numberseg = m_pSelectSeg->value();
if (m_numberseg == 3)
{
//hide table rows
m_pSegTable->setRowHidden(0, false);
m_pSegTable->setRowHidden(1, false);
m_pSegTable->setRowHidden(2, false);

for (int i=3; i<8; i++)
{
m_pSegTable->setRowHidden(i, true);
}
}
}

jpn
30th June 2008, 09:28
QTableWidget is a QAbstractScrollArea. The sizeHint() of QTableWidget doesn't take the contents anyhow into account. In fact, QTableWidget, QTableView or QAbstractItemView doesn't even implement sizeHint(). In other words, item views basically don't ask for any specific size because they're happy with what they get. If it's less than the contents require, scroll bars appear.

If you want a QTableWidget size to follow its contents, you should subclass QTableWidget and reimplement sizeHint(). QTableView and QHeaderView provide necessary functions to calculate size of the contents. You might also want to search the forums for more details. This subject has been discussed before.