Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass QSqlTableModel::primaryKey().index to QSqlTableModel.setHeaderData(index) to set an icon ?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 458 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #1

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

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

    https://github.com/sonichy

    sonichyS 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • sonichyS sonichy

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

        QSqlIndex sqlIndex = tableModel->primaryKey();
        //int index = sqlIndex
        tableModel->setHeaderData(index, Qt::Horizontal, QVariant::fromValue(QIcon(":/key.png")), Qt::DecorationRole);
        
        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @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);
                }
            }
        }
        

        https://github.com/sonichy

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved