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. How can i modify data in QML tableview
Forum Update on Monday, May 27th 2025

How can i modify data in QML tableview

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 3.5k 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.
  • N Offline
    N Offline
    NCsoft
    wrote on 14 Nov 2015, 11:57 last edited by
    #1

    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
    0
    • N Offline
      N Offline
      NCsoft
      wrote on 14 Nov 2015, 12:08 last edited by
      #2

      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
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 14 Nov 2015, 21:57 last edited by SGaist
        #3

        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 15 Nov 2015, 03:29
        0
        • S SGaist
          14 Nov 2015, 21:57

          Hi and welcome to devnet,

          Are you looking for something like this wiki article ?

          N Offline
          N Offline
          NCsoft
          wrote on 15 Nov 2015, 03:29 last edited by
          #4

          @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
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 15 Nov 2015, 20:42 last edited by
            #5

            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 16 Nov 2015, 01:31
            0
            • S SGaist
              15 Nov 2015, 20:42

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

              N Offline
              N Offline
              NCsoft
              wrote on 16 Nov 2015, 01:31 last edited by
              #6

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

              1/6

              14 Nov 2015, 11:57

              • Login

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