Important: Please read the Qt Code of Conduct -

How to pass QSqlTableModel::primaryKey().index to QSqlTableModel.setHeaderData(index) to set an icon ?


    QSqlIndex sqlIndex = tableModel->primaryKey();
    //int index = sqlIndex
    tableModel->setHeaderData(index, Qt::Horizontal, QVariant::fromValue(QIcon(":/key.png")), Qt::DecorationRole);

  • Lifetime Qt Champion


    I think the simpler way of doing that is to use a QIdentityProxyModel. Reimplement the headerData method and return the custom values you want for the colums you want to modify and the original values for the others.

  • @sonichy Solved !

    QSqlIndex sqlIndex = tableModel->primaryKey();
    qDebug() << sqlIndex;
    for (int i=0; i<sqlIndex.count(); i++) {
        for (int c=0; c<tableModel->columnCount(); c++) {
            QString header = tableModel->headerData(c, Qt::Horizontal, Qt::DisplayRole).toString();
            if (header == sqlIndex.fieldName(i)) {
                tableModel->setHeaderData(c, Qt::Horizontal, QVariant::fromValue(QIcon(":/key.png")), Qt::DecorationRole);

Log in to reply