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->setTable("tablename");
    m_modelo->setEditStrategy(QSqlTableModel::OnManualSubmit);	
    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);
    m_modelo->select();	
    m_ui.tableview->setModel(m_modelo);


  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.