简体   繁体   中英

QHeaderView only accounts for items currently on screen when resizing in ResizeToContents mode

I have a column in my QTableWidget whose contents are of variable length. It's important that the entirety of this content is visible (no ...), but I want to provide only as much space as is required. Here is the code I have to accomplish this.

... In the form's constructor

ui->myTable->horizontalHeader()->setResizeMode( 1, ResizeToContents );
ui->myTable->horizontalHeader()->setStretchLastSection( true );

...

This works with the exception that the specified column is sized to fit the largest item which is currently visible (on screen), meaning that any would be wider columns are left to narrow, and end with "..." and truncated content. Alternatively, I've tried the following:

... Populate the table ...

ui->myTable->resizeColumnToContents( 1 );

...

Unfortunately this seems to behave the same as the first code snippet, only considering visible columns when resizing.

How can I get the column to resize considering all items in the row, not only those that are visible?

Since you have tagged this as qt4 , I suspect this to be caused by a bug as seen here: https://bugreports.qt.io/browse/QTBUG-4206

This bug causes the table to only resize the items that are currently in its viewport and is fixed for versions >= 5.2. You might solve this in subclassing QHeaderView, but this seems like a rather exhausting approach. If feasible, I would suggest you switch to a newer Qt version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM