How can i modify data in QML tableview



  • Hi
    i used a sqltablemodel in c++ and a tableview in qml, and i use a textinput as delegates.
    Now the data in mysql database can displayed in tableview,and i can edit data in tableview,but how i can save the data that i edit in tableview into database?



  • Here is the delegate code:
    Component {
    id: myDelegate
    Button{
    width: ddddd.width
    height: 20
    MouseArea {
    onDoubleClicked: {
    test.selectAll()
    }
    }
    TextInput {
    id:test
    anchors.fill:parent
    selectByMouse: true
    text: styleData.value
    onAccepted: {
    }
    }
    }
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you looking for something like this wiki article ?



  • @SGaist said:

    devnet

    THAN
    i saw this article before.this article shows how i display mysql data in tableview,but can't edit mysql data in tableview.


  • Lifetime Qt Champion

    You have also to implement setData to handle the custom roles for your model



  • @SGaist
    hello i've redefine setData in my model,but it still can't work.
    Here is my code.

    • DbTableModel.h
      class DbTableModel : public QSqlTableModel
      {
      Q_OBJECT
      private:
      QHash<int, QByteArray> roles;
      void generateRoleNames();
      public:
      explicit DbTableModel(const DbTableModel &other, QObject *parent = 0);
      explicit DbTableModel(QObject *parent = 0);
      ~DbTableModel();
      Q_INVOKABLE QVariant data(const QModelIndex &index, int role=Qt::DisplayRole ) const;
      Q_INVOKABLE void saveData();
      Q_INVOKABLE bool setData(int row, int column, const QVariant value);
      Qt::ItemFlags flags(const QModelIndex &index) const ;
      virtual void setTable ( const QString &tableName );
      virtual QHash<int, QByteArray> roleNames() const;
      QModelIndex index1;
      };
    • DbTableModel.cpp
      DbTableModel::DbTableModel(const DbTableModel &other, QObject *parent)
      : QSqlTableModel(parent,other.database())
      {
      index1 = QModelIndex();

    }

    DbTableModel::DbTableModel(QObject *parent)
    : QSqlTableModel(parent)
    {

    }

    DbTableModel::~DbTableModel()
    {

    }

    QVariant DbTableModel::data ( const QModelIndex & index, int role ) const
    {

    if(index.row() >= rowCount())
    {
        return QString("");
    }
    if(role < Qt::UserRole)
    {
        return QSqlTableModel::data(index, role);
    }
    
    QModelIndex modelIndex = this->index(index.row(), role - Qt::UserRole - 1 );
    return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    

    }

    void DbTableModel::generateRoleNames()
    {
    roles.clear();
    for (int i = 0; i < columnCount(); i++)
    {
    roles[Qt::UserRole + i + 1] = QVariant(headerData(i, Qt::Horizontal).toString()).toByteArray();
    }
    }

    QHash<int, QByteArray> DbTableModel::roleNames() const
    {
    return roles;
    }

    void DbTableModel::setTable (const QString &tableName )
    {
    QSqlTableModel::setTable(tableName);
    generateRoleNames();
    }

    void DbTableModel::saveData()
    {
    this->database().transaction();
    this->database().commit();
    }

    bool DbTableModel::setData(int row, int column, const QVariant value)
    {
    int role = Qt::UserRole + 1 + column;
    return QSqlTableModel::setData(index(row,role - Qt::UserRole - 1), value, role);
    }

    Qt::ItemFlags DbTableModel::flags(const QModelIndex &index) const
    {
    return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
    }

    And i call setData(styleData.row,styleData.column,myText.text) in QML.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.