[SOLVED] Widget size after being added to a layout.
-
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. -
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.
-
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;
}
@ -
@raven-worx, your solution worked. Thanks!