QTableView horizontalHeader()->setStretchLastSection(true) and resizeColumnsToContents
-
Hi there.
I'm confusing in this situation: There's a QTableView in GUI. And it takes my custom table model. When GUI is shown, my program loads data from rest api to the table:QJsonArray arr = QJsonDocument::fromJson(reply->readAll()).array(); for(int i = 0; i < arr.size(); ++i) { QJsonObject obj = arr[i].toObject(); modelInvoiceItem->insertProduct(obj); } ui->tableInvoiceItem->resizeColumnsToContents(); ui->tableInvoiceItem->horizontalHeader()->setStretchLastSection(true);but the last horizontal header doesn't stretch.
-
@JonB yes, it's in a layout
@Abdurahman_Gulamkadirov
Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment outresizeColumnsToContents();while you see how it behaves?Also, I don't think
resizeColumnsToContents();has any effect until the table view is shown/resized. I think it's usually used in widget'sresizeorresizeEvent. -
Hi there.
I'm confusing in this situation: There's a QTableView in GUI. And it takes my custom table model. When GUI is shown, my program loads data from rest api to the table:QJsonArray arr = QJsonDocument::fromJson(reply->readAll()).array(); for(int i = 0; i < arr.size(); ++i) { QJsonObject obj = arr[i].toObject(); modelInvoiceItem->insertProduct(obj); } ui->tableInvoiceItem->resizeColumnsToContents(); ui->tableInvoiceItem->horizontalHeader()->setStretchLastSection(true);but the last horizontal header doesn't stretch.
@Abdurahman_Gulamkadirov
I don't know, but does it make any difference if you swap the order of the last two lines? -
@Abdurahman_Gulamkadirov
I don't know, but does it make any difference if you swap the order of the last two lines?@JonB No, it didn't help
-
@JonB No, it didn't help
@Abdurahman_Gulamkadirov
Like all other widgets, yourQTableViewis on a layout, isn't it? Nothing will work unless it is. -
@Abdurahman_Gulamkadirov
Like all other widgets, yourQTableViewis on a layout, isn't it? Nothing will work unless it is.@JonB yes, it's in a layout
-
This post is deleted!
-
@JonB yes, it's in a layout
@Abdurahman_Gulamkadirov
Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment outresizeColumnsToContents();while you see how it behaves?Also, I don't think
resizeColumnsToContents();has any effect until the table view is shown/resized. I think it's usually used in widget'sresizeorresizeEvent. -
@Abdurahman_Gulamkadirov
Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment outresizeColumnsToContents();while you see how it behaves?Also, I don't think
resizeColumnsToContents();has any effect until the table view is shown/resized. I think it's usually used in widget'sresizeorresizeEvent.@JonB I tested it after inserting row:
qInfo() << ui->tableInvoiceItem->horizontalHeader()->sectionResizeMode( ui->tableInvoiceItem->horizontalHeader()->count() - 1);it shows QHeaderView::Stretch. But the last section doesn't fill extra spaces. When I resize window, it automatically fills.
-
@JonB setting
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);helped, thanks a lot!