QTableView vertical header resize
-
Hi!
I try to resize the verticalheader of a qtableview with setFixedWidth(w) in runtime.
It works fine, but the grid not aligned after the resizing. I mean, when I increase the width of the header, it covers the left part of the first column. I think I need to repaint the tableview, but I don't know how.Here is my code:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QStringListModel *m = new QStringListModel(this); ui->tableView->setModel(m); m->setStringList({"a", "b"}); // here it works ui->tableView->verticalHeader()->setFixedWidth(20); connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on1); connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on2); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on1() { // here not ui->tableView->verticalHeader()->setFixedWidth(50); } void MainWindow::on2() { // here not ui->tableView->verticalHeader()->setFixedWidth(10); }
initial state:
https://mega.nz/#!Dk0gxIDK!0F8E8PzO-r7MKqi2MlVgznIa7akwfqw-kysPsxFGYNUafter on1:
https://mega.nz/#!yk9DUTBC!ikm3cxMNNlYjsCq_AXf0uI44jfZVLHGw8F6ccbFQ-3Yafter on2:
https://mega.nz/#!mxEEWYTL!5PjGLSph3LGgas0ezSUimNgAhsQ4aipDjKWqR96_RggThanks in advance:
Imre Horvath -
@imre.horvath
ui->tableView->setFixedWidth(50); is not a same ui->tableView->verticalHeader()->setFixedWidth(50); ? -
@imre.horvath
You are right sorry. on1 and on2 slot is called when enter a button? -
@imre.horvath
This seems to be a known issue withQTableView
A simple workaround from https://forum.qt.io/topic/8370/solved-resizing-vertical-header
is as follows:ui->tableView->verticalHeader()->setFixedWidth(50); int colw=ui->tableView->columnWidth(0); ui->tableView->setColumnWidth(0,colw-1); ui->tableView->setColumnWidth(0,colw);
In the post above, there is a more elegant method involving the use of
QMetaObject::invokeMethod()
which is the only way to invoke the protected method
updateGeometries()
which is apparently needed. -
@Diracsbracket said in QTableView vertical header resize:
int colw=ui->tableView->columnWidth(0);
ui->tableView->setColumnWidth(0,colw-1);
ui->tableView->setColumnWidth(0,colw);Thank you, it works.
Imre