How to set row height of QTableView?

48,418

For Qt versions < 5

QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);

For Qt versions >= 5 use

QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);

If that function doesn't apply to vertical headers, you likely will have to call setRowHeight() every time you add a new row.

Share:
48,418
Ashot
Author by

Ashot

Updated on July 09, 2022

Comments

  • Ashot
    Ashot almost 2 years

    I have QTableView and QAbstractTableModel. I require rows to have height equal to 24. I know the only way to do this is by calling QTableView::setRowHeight. Since the model is dynamic it may be added new rows, but I don't want to call setRowHeight each time new row is added.

    How can I configure QTableView such that it uses the same height for new added rows or can a model be sent the height of rows?

  • Llopeth
    Llopeth almost 5 years
    I think it's better to give a name to the pointer different from the function name, otherwise you may have name conficts, e.g. if you do this from QTableView constructor QHeaderView *vh = myTableView->verticalHeader();
  • Cory Klein
    Cory Klein over 4 years
    If verticalHeader is already in scope, then just call setResizeMode and setDefaultSectionSize on it directly, no need to be worried about variable shadowing and no need to copy a local pointer.
  • MattBas
    MattBas almost 4 years
    QHeaderView bounds the value passed to setDefaultSectionSize so you might need to call setMinimumSectionSize and setMaximumSectionSize before setting the default size.