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
Forum Updated to NodeBB v4.3 + New Features

Unable to edit QSqlTableModel from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qsqltablemodelsqlitemodelmodel-view
4 Posts 2 Posters 877 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.
  • D Offline
    D Offline
    daljit97
    wrote on 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
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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 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
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on 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

          • Login

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