Congratulations to our 2022 Qt Champions!

[solved]resizing vertical header

  • I have two QTableViews
    I want the vertical header of Table2 to be wide as much as vertical header width of Table1 + first column width of Table1, so that the first column of Table2 starts in the same point of second column of Table1.
    I've tried the resize() function of QHeaderView but using that the header covers the first column instead of pushing it to the right.
    what can i do?
    I cannot add a column to the model.

  • can you show us a picture of what you want?

  • ! i want to bring the header)!

    I need the header to reach the black mark I made in the picture.

  • there is a problem with your image link. it doesn't show up.

  • sorry, corrected (I hope :D)

  • "The frozen column example": is not exactly what you want, but the same techniques can be used here.

    There are also 2 QTables that need to be in sync with each orther.

  • You can try "QTableView::setColumnWidth": and set the corresponding width.

  • I think Paul wants something like this :
    ! tables)!

    Is that correct?

  • yes!
    I solved now inheriting from qheaderview and using a method to set sizehint's width, and I override sizeHint() to return a Qsize with parent's height and just set width. I consider this an ugly fix, but it works.

    what's your solution (probably better)?

  • I didn't have a solution 13 minutes ago. I was just testing some things for which a made a ui.

    I don't know if it's better, but I didn't need to subclass :
    in the constructor of the form/mainwindow where you use the 3 tables :

    @ qDebug() << ui->tableWidget->verticalHeader()->sizeHint().width(); //get width of VHeader
    qDebug() <<ui->tableWidget->columnWidth(0);//get width of first column

    int HeaderWidth2 = ui->tableWidget->verticalHeader()->sizeHint().width() + ui->tableWidget->columnWidth(0);
    ui->tableWidget_2->verticalHeader()->setFixedWidth(HeaderWidth2);//set width of second VHeader@

    This doen't take in to account that the widths could be changed afterwards. In that case we will need some signals and slots.

    Anyway I hope it helps in some way.

  • Well, your solution is better because you haven't subclassed, but it requires the same workaround when resizing: when I resize the header (with signal/slot stuff), in my and your solution the first column ("one" in your example) is not pushed to the right.
    instead, the header covers the column.

    As an ugly fix, I have to do something like
    @ int colw=ui->tblTotals->columnWidth(0);

    and the table is repainted correctly.
    thanks anyway.


  • I understand now what you mean about the column resizing thing.

    Glad it is working!

    Have Qt fun!

  • I think you have to call the slot QTableView::updateGeometries() after resizing the header.
    Because that slot is protected, you have to call it through QMetaObject::invokeMethod.
    Assuming the 2 tables are named ui->tblHours and ui->tblTotals:
    @// In the constructor
    // The real column resizing is done asynchronously,
    // so you have to delay the slot with Qt::QueuedConnection
    connect(ui->tblHours->horizontalHeader(), SIGNAL(sectionResized(int,int,int)),
    SLOT(resizeHeader()), Qt::QueuedConnection);

    void YourClass::resizeHeader() {
    int width = ui->tblHours->verticalHeader()->width() +
    QMetaObject::invokeMethod(ui->tblTotals, "updateGeometries");

  • thanks a lot alexisdm. this worked for me. this approach has the advantage to be more clear.
    very strange thing the QMetaObject one, it breaks everything about protected/private and so on. I think in a theorical OOP it has to be avoided, but for actual programming is very useful :D

  • @Alexis

    good one!

Log in to reply