Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How in QSqlTableModel change all value in the column?
Qt 6.11 is out! See what's new in the release blog

How in QSqlTableModel change all value in the column?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    How in QSqlTableModel change all value in the column?
    I could change the values by index, but

    tableModel->rowCount();
    

    return 256, whereas I have 86,000 values.

    Christian EhrlicherC 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      How in QSqlTableModel change all value in the column?
      I could change the values by index, but

      tableModel->rowCount();
      

      return 256, whereas I have 86,000 values.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mikeeeeee Do you know the search function?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Mikeeeeee Do you know the search function?

        M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        @Christian-Ehrlicher

            while (tableModel->canFetchMore())
             tableModel->fetchMore();
        

        this is how I get my 86,000 rows.
        But how do I replace all the cells? This only changes the first cell.

            for (int i = 0; i < 10; i++) {
                index = tableModel->index(i, 0);
                tableModel->setData(index, "sdaf");
            }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mikeeeeee
          wrote on last edited by
          #4

          I tried to make my own model, with 3 roles, but when updating the model, the kernel requests roles from 0 to 10. What might be the bug?

          JonBJ 1 Reply Last reply
          0
          • M Mikeeeeee

            I tried to make my own model, with 3 roles, but when updating the model, the kernel requests roles from 0 to 10. What might be the bug?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Mikeeeeee
            Your questions jump all over the place. You ask about 256/86,000 rows. Now you are asking about "roles", and "the kernel requests roles from 0 to 10.". And "I tried to make my own model, with 3 roles,".

            What are you doing with roles, what do you actually want to set? tableModel->setData(index, "sdaf"); is setting the Qt::EditRole (i.e. just what you think of as the "value" of the data). What else are actually trying to achieve? Please explain in intelligible language, we can't guess.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              it's return:

                  for (int i = 0; i < 10; i++) {
                      //qDebug()<<i;
                      index = tableModel->index(i, 0);
                      qDebug()<<index;
                      //tableModel->setData(index, "sdaf");
                      tableModel->setData(index, QDateTime::fromTime_t(tableModel->data(index).toLongLong()).toString("dd-MM-yyyy hh:mm:ss"));
                      qDebug()<<tableModel->data(index);
                  }
              
              QModelIndex(0,0,0x0,QSqlTableModel(0x367f70))
              QVariant(QString, "01-01-1970 03:00:00")
              QModelIndex(1,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820738)
              QModelIndex(2,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820739)
              QModelIndex(3,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820740)
              QModelIndex(4,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820741)
              QModelIndex(5,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820742)
              QModelIndex(6,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820743)
              QModelIndex(7,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820744)
              QModelIndex(8,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820745)
              QModelIndex(9,0,0x0,QSqlTableModel(0x367f70))
              QVariant(qlonglong, 1564820746)
              

              Why changes only first?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                If I do this in the model, I get strange squares. What should I do with these squares? How do I remove squares?

                QVariant MyModel::data(const QModelIndex & index, int role) const {
                     return QVariant(QString("row = ") + QString::number(index.row()) + "  col = " + QString::number(index.column()));
                }
                

                111.png

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by
                  #8

                  it is work

                  #include "mymodel.h"
                  
                  MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
                  {
                  
                  }
                  
                  QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
                  {
                      if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
                      {
                          switch (section)
                          {
                          case 0:
                              return "Время";
                          case 1:
                              return "Значение";
                          case 2:
                              return "Группа";
                          default:
                              return QVariant();
                          }
                      }
                      return QAbstractTableModel::headerData(section, orientation, role);
                  
                  }
                  
                  void MyModel::addRow(QString str1, long long val2, int val3)
                  {
                      beginInsertRows( QModelIndex(), 0, 2 );
                      dateTime.append(str1);
                      value.append(val2);
                      number.append(val3);
                      endInsertRows();
                  }
                  
                  int MyModel::columnCount(const QModelIndex &parent) const
                  {
                      return 3;
                  }
                  
                  int MyModel::rowCount(const QModelIndex & parent) const {
                      Q_UNUSED(parent);
                      return dateTime.count();
                  }
                  
                  QVariant MyModel::data(const QModelIndex & index, int role) const {
                      if (role == Qt::DisplayRole)
                          {
                      if(index.column() == 0){
                          return QVariant(dateTime[index.row()]);
                      }
                      if(index.column() == 1){
                          return QVariant(value[index.row()]);
                      }
                      if(index.column() == 2){
                          return QVariant(number[index.row()]);
                      }
                      }
                      return QVariant();
                  }
                  
                  QHash<int, QByteArray> MyModel::roleNames() const {
                      QHash<int, QByteArray> roles;
                      roles[Role1] = "role1";
                      roles[Role2] = "role2";
                      roles[Role3] = "role3";
                      return roles;
                  }
                  
                  void MyModel::updateMyModel(QString nameTable)
                  {
                      dateTime.clear();
                      dateSecs.clear();
                      value.clear();
                      number.clear();
                      QSqlQuery query;
                      query.prepare("SELECT "
                                    "Time,"
                                    "Value,"
                                    "Number"
                              " FROM " + nameTable + ";");
                  
                      if (!query.exec()) {qDebug()<<"не получило данные";}
                      else
                      {
                          qDebug()<<"получило данные";
                          query.first();
                          dateTime.append(QDateTime::fromTime_t(query.value(0).toLongLong()).toString("dd-MM-yyyy hh:mm:ss"));
                          dateSecs.append(query.value(0).toLongLong());
                          value.append(query.value(1).toDouble());
                          number.append(query.value(2).toInt());
                          while (query.next()) {
                              dateTime.append(QDateTime::fromTime_t(query.value(0).toLongLong()).toString("dd-MM-yyyy hh:mm:ss"));
                              dateSecs.append(query.value(0).toLongLong());
                              value.append(query.value(1).toDouble());
                              number.append(query.value(2).toInt());
                          }
                      }
                  }
                  
                  void MyModel::startDb(QString pathTable)
                  {
                      myDatabase = QSqlDatabase::addDatabase("QSQLITE");
                      myDatabase.setDatabaseName(pathTable);
                      if (myDatabase.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
                  }
                  
                  
                  
                  
                  
                  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