Sizing QTableview to its content
-
Hello,
I have a QTableView which content (nb of columns) can change programatricaly. I would like to adjust the size of the tableview to the content.
Below you can see the result I have so farIn Qt Creator it looks like below
How can I adjust the size of the tableview to fit to the content?
Thank you -
Hi,
You have to resize it yourself by requesting its sizeHint after you modify the number of columns.
-
@Bert59 said in Sizing QTableview to its content:
size.setWidth(350);
What do expect this to do? It just changes width in QSize instance "size" nothing more - it does not resize anything.
-
I would like that a tableview keeps the size of it's content.
If I add columns the tableview should expand to show all columns as long as it is smaller than the window.
If I resize the window the tableview should keep the same size and should stay centered in the window.
For testing the possibility of setting the width of the tableview to a given value I tried the following code:ui->table1->setModel(&model); //table1 is the name of the tableview
model.insertRows(0, 4);
model.insertColumns(0, 4);int size = ui->table1->sizeHint().width(); qInfo() << size; //Result is 256 ui->table1->sizeHint().setWidth(350); size = ui->table1->sizeHint().width(); qInfo() << size; //Result remains 256
I couldn' find in the documentation how to achive this behaviour.
I hope this clarfies what I'm looking for. -
@Bert59
What I/ @jsulm meant was: you can't just alter theQSize
returned by asizeHint()
method and expect that to alter a widget's size hint!You were supposed to use methods like
QWidget::setMinimumSizeHint()
orQWidget::resize()
....Whether that will do all that you want I do not know. But that is what @SGaist meant by
You have to resize it yourself by requesting its sizeHint after you modify the number of columns.
-
Hi,
I got the behaviour I was looking for by using the following code.model.insertColumns(0, 4); ui->table1->setModel(&model); int colWidth = ui->table1->columnWidth(1); int vHeaderWidth = ui->table1->verticalHeader()->width(); int tableWidth = vHeaderWidth + 4*colWidth; ui->table1->setFixedWidth(tableWidth);
As I'm a beginner with Qt I don't know if it is the recommended way of doing it, but for the time being it works as expected.
Thank you for your comments