Qt6 topics merged with General

Text and Icon in QTableView header

  • Hi,

    I would like to have text and icons in the horizontal header of QTableView.
    With the following code I can display text header but I don't know how to add icons to each text:

       m_modelo = new QSqlRelationalTableModel(this);	
    m_modelo->setHeaderData(1, Qt::Horizontal,tr("ORDER"));
    m_modelo->setHeaderData(2, Qt::Horizontal,tr("PLAYER"));
    m_modelo->setHeaderData(3, Qt::Horizontal,tr("NUMBER"));
    m_modelo->setSort(1, Qt::AscendingOrder);

  • Suppose you want to add an icon to the very first column (first section - index 0 - of horizontal headers).

    m_modelo->setHeaderData(0, Qt::Horizontal, QVariant::fromValue(QIcon("path/to/icon")), Qt::DecorationRole);

    You can adapt the code by adjusting either the targeted section (i.e. the column or the row depending on the context) or the targeted header (depending on the given orientation parameter).

Log in to reply