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 To Change data (setData) in EditableModel child on QSqlQueryModel ?
Forum Updated to NodeBB v4.3 + New Features

How To Change data (setData) in EditableModel child on QSqlQueryModel ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 5.0k Views 2 Watching
  • 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.
  • Taz742T Taz742

    @mrjj Yes, it saves new data in database but form shows old numbers

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @Taz742

    Ok, maybe you need to use the
    http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsAboutToBeRemoved
    or some other function that fits the action you do to the data.

    Currently, you just change the database behind its back
    so I guess what is wrong is that it cannot know that. :)

    Taz742T 1 Reply Last reply
    0
    • mrjjM mrjj

      @Taz742

      Ok, maybe you need to use the
      http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsAboutToBeRemoved
      or some other function that fits the action you do to the data.

      Currently, you just change the database behind its back
      so I guess what is wrong is that it cannot know that. :)

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #7

      @mrjj
      It does not have setQuery();

      Do what you want.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #8

        QAbstractItemModel is the lowest base class of QSqlQueryModel.

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

        Taz742T 1 Reply Last reply
        0
        • SGaistS SGaist

          QAbstractItemModel is the lowest base class of QSqlQueryModel.

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #9

          @SGaist
          you are right.

          So, I think it would be better TableWidget. I fill in the data manually.

          What do you think? @SGaist , @mrjj

          Do what you want.

          Taz742T 1 Reply Last reply
          0
          • Taz742T Taz742

            @SGaist
            you are right.

            So, I think it would be better TableWidget. I fill in the data manually.

            What do you think? @SGaist , @mrjj

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #10

            I solved my problem.

            QSqlQuery query;
            
            query.prepare("SELECT * FROM VALUTA");
            
            query.exec();
            
            ui->tableWidget->setColumnCount(3);
            ui->tableWidget->setRowCount(8);
            ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "იდ" << "სახელი" << "კურსი");
            ui->tableWidget->verticalHeader()->hide();
            
            int i = 0;
            
            while(query.next()){
                int j = 0;
                while(j < 3){
                    QTableWidgetItem* itm = new QTableWidgetItem(query.value(j).toString());
                    ui->tableWidget->setItem(i,j,itm);
            
                    if(j == 0){
                        itm->setFlags(itm->flags() ^ Qt::ItemIsEditable); // **DISABLE EDIT**
                    }
            
                    j++;
                }
                i++;
            }
            

            But the concrete problem has not been reached.
            I write as resolved?

            Do what you want.

            mrjjM 1 Reply Last reply
            0
            • Taz742T Taz742

              I solved my problem.

              QSqlQuery query;
              
              query.prepare("SELECT * FROM VALUTA");
              
              query.exec();
              
              ui->tableWidget->setColumnCount(3);
              ui->tableWidget->setRowCount(8);
              ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "იდ" << "სახელი" << "კურსი");
              ui->tableWidget->verticalHeader()->hide();
              
              int i = 0;
              
              while(query.next()){
                  int j = 0;
                  while(j < 3){
                      QTableWidgetItem* itm = new QTableWidgetItem(query.value(j).toString());
                      ui->tableWidget->setItem(i,j,itm);
              
                      if(j == 0){
                          itm->setFlags(itm->flags() ^ Qt::ItemIsEditable); // **DISABLE EDIT**
                      }
              
                      j++;
                  }
                  i++;
              }
              

              But the concrete problem has not been reached.
              I write as resolved?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Taz742
              If you still have the view issue then leave it open :)

              Taz742T 1 Reply Last reply
              0
              • mrjjM mrjj

                @Taz742
                If you still have the view issue then leave it open :)

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #12

                @mrjj
                Yes, I will leave open. I think This topical theme.

                Do what you want.

                1 Reply Last reply
                0
                • RajeeshRaveendranR Offline
                  RajeeshRaveendranR Offline
                  RajeeshRaveendran
                  wrote on last edited by
                  #13

                  Hi,

                  Did you forget to emit dataChanged() signal after value updated?
                  This signal is must if you need to reflect the changes on UI.

                  Taz742T 1 Reply Last reply
                  0
                  • RajeeshRaveendranR RajeeshRaveendran

                    Hi,

                    Did you forget to emit dataChanged() signal after value updated?
                    This signal is must if you need to reflect the changes on UI.

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by
                    #14

                    Hi. @RajeeshRaveendran
                    YES, I've seen this signal.
                    But UI->, update, when it changes, model-> setData doing nothing.

                    Do what you want.

                    mrjjM 1 Reply Last reply
                    0
                    • Taz742T Taz742

                      Hi. @RajeeshRaveendran
                      YES, I've seen this signal.
                      But UI->, update, when it changes, model-> setData doing nothing.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @Taz742
                      Did u try to call
                      emit dataChanged()
                      in setData ?

                      1 Reply Last reply
                      0
                      • RajeeshRaveendranR Offline
                        RajeeshRaveendranR Offline
                        RajeeshRaveendran
                        wrote on last edited by
                        #16

                        Hi Taz,

                        I did not get you. Let me explain a little more. Simply you should modify your model class to

                        bool EditAbleModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
                        {
                        /// update the data to database/internal data storage
                        emit dataChanged(); /// So that UI (QML) know the change occurred in background and query the new value by calling data() method to update UI.
                        }

                        QVariant EditAbleModel::data(const QModelIndex &index, int role)
                        {
                        /// return your data as per custom db.
                        }

                        Taz742T 1 Reply Last reply
                        1
                        • RajeeshRaveendranR RajeeshRaveendran

                          Hi Taz,

                          I did not get you. Let me explain a little more. Simply you should modify your model class to

                          bool EditAbleModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
                          {
                          /// update the data to database/internal data storage
                          emit dataChanged(); /// So that UI (QML) know the change occurred in background and query the new value by calling data() method to update UI.
                          }

                          QVariant EditAbleModel::data(const QModelIndex &index, int role)
                          {
                          /// return your data as per custom db.
                          }

                          Taz742T Offline
                          Taz742T Offline
                          Taz742
                          wrote on last edited by
                          #17

                          @RajeeshRaveendran
                          Now I'm busy with something else and definitely I'll see into your answer.
                          thank for reply, and sorry english :))

                          Do what you want.

                          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