Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unable to edit QSqlTableModel from QML

Unable to edit QSqlTableModel from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qsqltablemodelsqlitemodelmodel-view
4 Posts 2 Posters 834 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.
  • D Offline
    D Offline
    daljit97
    wrote on 19 Jul 2020, 16:24 last edited by
    #1

    I subclassed a class SqliteModel from QSqlTableModel. I want the model to be editable so reading the documentation I found that I need to reimplement the setData function. I implemented the function as below:

    bool SqliteModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.isValid()) {
            bool ret = QSqlTableModel::setData(index, value, Qt::EditRole);
            qDebug() << ret << lastError(); // debug output
            if(ret) dataChanged(index, index);
            return ret;
        }
        return false;
    }
    

    However this always return false and the debug output is: false QSqlError("20", "Unable to fetch row", "datatype mismatch"). I am using the model inside QML (the items are correctly displayed and when I clicke the setData function is correctly called):

        ListView{
            anchors.fill: parent
            model: sqlmodel
            delegate: Label{
                text: model.linecolor
                MouseArea{
                    anchors.fill: parent
                    onClicked:{
                        model.linecolor = "hello"
                    }
                }
            }
        }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 20 Jul 2020, 05:54 last edited by
      #2

      What is printed if you call:

      qDebug() << index.row() << value << role;
      

      Also, have you defined any role names? roleNames()

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on 20 Jul 2020, 11:08 last edited by daljit97
        #3

        I get this:
        false QSqlError("20", "Unable to fetch row", "datatype mismatch").
        The rolenames are defined and correctly displayed.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 20 Jul 2020, 11:13 last edited by
          #4

          @daljit97 said in Unable to edit QSqlTableModel from QML:

          , Qt::EditRole);

          Then you should probably pass the role and not editRole, otherwise model won't know which column to update.

          (Z(:^

          1 Reply Last reply
          0

          4/4

          20 Jul 2020, 11:13

          • Login

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