Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • https://github.com/sonichy/HTYSQL
    替代文字

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

  • Lifetime Qt Champion

    Hi,

    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