Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved How can i modify data in QML tableview

    QML and Qt Quick
    2
    6
    3027
    Loading More Posts
    • 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.
    • N
      NCsoft last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • N
        NCsoft last edited by

        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: {
        }
        }
        }
        }

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by SGaist

          Hi and welcome to devnet,

          Are you looking for something like this wiki article ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply Reply Quote 0
          • N
            NCsoft @SGaist last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              N 1 Reply Last reply Reply Quote 0
              • N
                NCsoft @SGaist last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post