Qt-interest Archive, August 2006
QTableWidget resizing when columns are resized
Message 1 in thread
Hi,
Is there a size policy (or any other mechanism) ensuring that, when the columns
of a QTableWidget are resized with resizeColumnsToContents(), the width of the
QTableWidget itself is also resized to match the new column widthes? Indeed,
the column widthes are properly resized, but the QTableWidget width remains
unchanged and there's extra space between the right border of the last column
and the QTableWidget border.
As a workaround, I compute the cumultative sum of the resized column widthes and
do a QTableWidget::setFixedSize() with the computed value. Is this the right way
to do this?
Thanks,
Émeric
--
[ signature omitted ]
Message 2 in thread
For Qt3, I had this problem also. That is, I resized a child widget
(hmm...it was even a QTable) and wanted everybody that contained it to also
resize to fit it. I ended up needing to do the following.
void assertWidgetSize ( QWidget* w )
{
for ( QObject* tmp = w ; tmp ; tmp = tmp->parent() ) {
if ( tmp->inherits( "QWidget" ) ) {
QWidget* cast = ( QWidget* )tmp;
cast->adjustSize();
}
}
}
Then I would call assertWidgetSize( myTableHere ); after resizing my table.
I know it's old-style bad-idea typecasting; don't bug me, man. ;)
Nathan
On 8/14/06 6:37 AM, "Émeric Maschino" <emeric.maschino@xxxxxxxxxxxx> wrote:
> Hi,
>
> Is there a size policy (or any other mechanism) ensuring that, when the
> columns
> of a QTableWidget are resized with resizeColumnsToContents(), the width of the
> QTableWidget itself is also resized to match the new column widthes? Indeed,
> the column widthes are properly resized, but the QTableWidget width remains
> unchanged and there's extra space between the right border of the last column
> and the QTableWidget border.
>
> As a workaround, I compute the cumultative sum of the resized column widthes
> and
> do a QTableWidget::setFixedSize() with the computed value. Is this the right
> way
> to do this?
>
> Thanks,
>
> Émeric
>
--
[ signature omitted ]
Message 3 in thread
Hi,
> For Qt3, I had this problem also. That is, I resized a child widget
> (hmm...it was even a QTable) and wanted everybody that contained it to also
> resize to fit it. I ended up needing to do the following.
>
> void assertWidgetSize ( QWidget* w )
> {
> for ( QObject* tmp = w ; tmp ; tmp = tmp->parent() ) {
> if ( tmp->inherits( "QWidget" ) ) {
> QWidget* cast = ( QWidget* )tmp;
> cast->adjustSize();
> }
> }
> }
>
> Then I would call assertWidgetSize( myTableHere ); after resizing my table.
> I know it's old-style bad-idea typecasting; don't bug me, man. ;)
For new-style typecasting, I would suggest qobject_cast<QWidget*>(tmp).
For the rest, I still don't know if there's a Qt trick to automatically
resize a QTableWidget once the columns are resized...
Ãmeric
--
[ signature omitted ]
Message 4 in thread
> Is there a size policy (or any other mechanism) ensuring that, when
> the columns
> of a QTableWidget are resized with resizeColumnsToContents(), the
> width of the
> QTableWidget itself is also resized to match the new column widthes?
> Indeed,
> the column widthes are properly resized, but the QTableWidget width
> remains
> unchanged and there's extra space between the right border of the last
> column
> and the QTableWidget border.
>
> As a workaround, I compute the cumultative sum of the resized column
> widthes and
> do a QTableWidget::setFixedSize() with the computed value. Is this the
> right way
> to do this?
Unfortunately, it seems that resizeColumnsToContents() only takes care
of the horizontal header's contents, not the cells :-(
--
[ signature omitted ]
Message 5 in thread
> Unfortunately, it seems that resizeColumnsToContents() only takes care
> of the horizontal header's contents, not the cells :-(
Nevermind. This was due to my custom delegate for editing the
QTableWidget cells: I forget to override
QItemDelegate::updateEditorGeometry().
Sorry for the noise.
Ãmeric
--
[ signature omitted ]
Message 6 in thread
Hi,
I finally found the solution: derive my widget from QTableWidget and
override sizeHint() and minimumSizeHint():
QSize myTableWidget::minimumSizeHint() const
{
QSize size( QTableWidget::sizeHint() );
int width = 0;
for (int c = 0; c < columnCount(); ++c)
width += columnWidth( c );
size.setWidth( width + 4 );
return size;
}
QSize myTableWidget::sizeHint() const
{
return minimumSizeHint();
}
I didn't use any size policy. In the main code, I just do:
table.resizeColumnsToContents();
table.setMinimumSize( table.minimumSizeHint() );
where table is a myTableWidget object.
As you may have noticed, I add 4 to the computed width in the overridden
minimumSizeHint() method. Otherwise, the width is too small and I get
the horizontal scrollbar. Is there anybody out there knowing where this
4 comes from? I must add the same quantity to properly resize the
minimum width of a QListWidget-derived object.
Ãmeric
--
[ signature omitted ]