Question

I've got a QTableView with a custom model, and it works fine. However, when I start the program, I want the rows and columns to be sized to fit because by default they take up too much space. I should note that my table has 65k rows (only 3 columns), and they're always the same. The following code works fine, but is very slow.

memorListView is a QTableView. Prior to this line, I setModel to my custom model

memoryListView->resizeRowsToContents();
memoryListView->resizeColumnsToContents();

Like I mentioned, this does exactly what I need, but it takes about 8 seconds to run. I presume it's because all the data needs to be pulled out of the model, when the view is otherwise happy to be lazy about requesting it.

I could set the row and column size manually, which is my current workaround, but this is a cross-platform app and I'd like to not have to worry about different widgets, text sizes, DPIs, etc causing a problem. I could autosize one row and then set the rest to that row's size, which would be correct but seems very hacky.

I'm hoping that I'm just doing this naively, and there's some option I can toggle that'll make it faster. I know all rows and columns will be the same size; can I tell it to use that hint somehow?

Thanks in advance.

Was it helpful?

Solution

You could simply measure the width of the text using QFontMetrics, then set the column width to it. Something like that should work:

QFontMetrics fontMetrics(yourWidget->font());
int columnWidth = fontMetrics.width("some text");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top