title: Subclassing QSqlTableModel insert new value
body: <p>I want to show SQL data from a local db-File in a QML-Tableview and than would like to do some edits to the sql-database.
  What I managed to do after about three weeks: Showing my data in a QML-Tableview. I am not sure if I really need to subclass QSqlTableModel and I definitly would be glad if subclassing would not be neccessary at all.</p>
  
  <p>In my main.cpp following should create my model and directly add a record.</p>
  
  <pre><code>    // Create an instance of the SqlModel for accessing the data
      SqlDataModel *sqlModel;
      sqlModel = new SqlDataModel(0,base.database());
      sqlModel-&gt;setTable("diaBaneDatabase");
      sqlModel-&gt;setSort(0, Qt::AscendingOrder);
      sqlModel-&gt;setEditStrategy(QSqlTableModel::OnFieldChange);
  
      sqlModel-&gt;select();
  
  
      QSqlRecord record(sqlModel-&gt;record());
      record.setValue(0,50);
      record.setValue(3,222);
      sqlModel-&gt;insertRecord(-1, record);
      sqlModel-&gt;submitAll();
  </code></pre>
  
  <p>This should add 222 to the 4th column. But nothing will be stored in my sqlDatabase</p>
  
  <p>My SqlDataModel::setData loolks like this:</p>
  
  <pre><code>    bool SqlDataModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role)
      {
          qDebug() &lt;&lt; index.column() &lt;&lt; "   " &lt;&lt; index.row() &lt;&lt; "   " &lt;&lt; value &lt;&lt; "   ----  " &lt;&lt; role;
  
  
          qDebug() &lt;&lt; roles[Qt::UserRole + 1];
  
          //qDebug() &lt;&lt; QSqlTableModel::setData(modelIndex, value);
  
         qDebug() &lt;&lt; QSqlQueryModel::setData(index, value);
          return false;
  
      }
  </code></pre>
  
  <p>The output will be:</p>
  
  <pre><code>    0     39     QVariant(int, 50)    ----   2
      "id"
      false
      1     39     QVariant(QString, "")    ----   2
      "id"
      false
      2     39     QVariant(QString, "")    ----   2
      "id"
      false
      3     39     QVariant(int, 222)    ----   2
      "id"
      false
      4     39     QVariant(double, 0)    ----   2
      "id"
      false
      5     39     QVariant(int, 0)    ----   2
      "id"
      false
      6     39     QVariant(double, 0)    ----   2
      "id"
      false
      7     39     QVariant(double, 0)    ----   2
      "id"
      false
  </code></pre>
  
  <p>For sure my setData method is wrong but I don't understand what should happen there and I didn't find any example for this.</p>
  
  <p>Am I wrong with my assumption that I need to subclass QSqlTableModel to be able to put the model through QQmlContext to QML and than show the columns with roles named like my column-namings? If not how could I put the content of column 1 to QMLTableview:</p>
  
  <pre><code>        TableViewColumn {
              role: "id" // what should be the role if I don't subclass???
              title: "ID"
              width: 80
          }
  </code></pre>
  
  <p>I'm happy for any help, comment, example, other posts or whatever brings me further forward ... thanks</p>