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 set data (Qt::UserRole) into QSqlQueryModel column?
Forum Update on Monday, May 27th 2025

How to set data (Qt::UserRole) into QSqlQueryModel column?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 8.5k Views
  • 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.
  • U Offline
    U Offline
    umen242
    wrote on 2 Feb 2012, 17:22 last edited by
    #1

    I have QSqlQueryModel that handling data , im trying to set Qt::UserRole to column
    but i can't figure a way to do it i have implement the data method :
    basically what i want is to hide the Qt::DisplayRole of ndex.column() 4 and set it to Qt::UserRole
    @QVariant MyListSqlModel::data(const QModelIndex &index, int role) const
    {
    QVariant value = QSqlQueryModel::data(index, role);
    QVariant valueEmpty = "";
    int j = index.column();
    if (value.isValid() && role == Qt::DisplayRole && j== 4 )
    {

    QModelIndex LinkIndex = QSqlQueryModel::index(index.row(),4);
    setData(LinkIndex,value,Qt::UserRole); // this is not working and gives me erorr

    return valueEmpty;
    }
    else
    {
    return value;
    }
    }@

    it give me this error that i know what it is the implement method is not const
    @ error C2662: 'MyListSqlModel::setData' : cannot convert 'this' pointer from 'const MyListSqlModel' to 'MyListSqlModel&'@

    but what is the right way to do it ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 2 Feb 2012, 23:48 last edited by
      #2

      data() is a const method. That means that you cannot modify the data of your class in that method (there are some tricks, but that's not recommended here). setData() modifies the data, this is the reason why you cannot call setData() within data().

      IMHO, that is not needed. I'm not 100% sure if I understood correctly what you need, but I think it would be sufficient to just return the value of the other role:

      @
      QVariant MyListSqlModel::data(const QModelIndex &index, int role) const
      {
      QVariant value = QSqlQueryModel::data(index, role);
      if(role == Qt::DisplayRole && index.column() == 4 && value.isValid())
      return QSqlQueryModel::data(QSqlQueryModel::index(index.row(),4), Qt::UserRole);
      else
      return value;
      }
      @

      This returns the regular value for all columns but the 5th (0 based indexes).

      If you're on column 4 and the DisplayRole value is valid, return the value of the UserRole instead, otherwise the original value.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on 3 Feb 2012, 07:32 last edited by
        #3

        Hi Thanks for the replay . its the right direction .. but i need to "Set"
        the DisplayRole value into Qt::UserRole value ,
        so later in the itemDelegate i could Query the data from the Qt::UserRole.
        now i just return empty value into Qt::UserRole.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 3 Feb 2012, 08:12 last edited by
          #4

          I doubt that QSqlQueryModel supports setting user data for any role. This class is designed to represent an SQL query result. I think it doesn't even implement setData, but you'd have to check that in the source. setData is implemented for QSqlTableModel, but that class will try to store the data in the database. I think neither is what you want.

          I would investigate a proxy model solution, that will allow you to "overlay" additional data on any underlying data model. You'd have to implement it yourself though.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on 3 Feb 2012, 08:25 last edited by
            #5

            Hi
            thanks for replay ,
            in the end all i want to do is to decorate item in the QSqlQueryModel Tableview
            so i now using allso ItemDelegate from QStyledItemDelegate
            that will overwrite the Text in the Item. i hope this will work .
            from what i do now it is not removing the old text dont know why ..
            this is the paint method in the item delegate:

            @if (index.isValid())
            {
            int j = index.column();
            if(j==4)
            {
            QString headerText_UserRole = index.data(Qt::UserRole).toString() ;
            QString headerText_DisplayRole = index.data(Qt::DisplayRole).toString() ;
            painter->save();
            QFont font = QApplication::font();
            font.setBold(true);
            painter->setFont(font);
            QRect headerRect = option.rect;

            painter->drawText(headerRect,"");
            painter->restore();
            }
            }

            QStyledItemDelegate::paint(painter, option, index);@

            1 Reply Last reply
            0

            1/5

            2 Feb 2012, 17:22

            • Login

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