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. Cant change cell data one below cell
Forum Updated to NodeBB v4.3 + New Features

Cant change cell data one below cell

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 2 Posters 2.8k 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.
  • H Offline
    H Offline
    hubeytqew
    wrote on last edited by hubeytqew
    #1

    Hi, I can change data in the cell which was fetched from database but only first row. I iterate table related with the rowCount() and columnCount() first row's datas changed as expected. When I went to the second row, data's didnt changed. I guess Should I set the index etc.?
    I use QSqlTableModel with setData method

    Both getstring and givestring are QString.

        for(int i=0; i<modal->rowCount(); i++)
        {
            for(int j=1; j<modal->columnCount(); j++)
            {
                getstring = ui->tableView->model()->data(ui->tableView->model()->index(i,j)).toString();  //get the string from table
                givestring = instance1.do_some_work(getstring, getstring.length()/2);                //change its variable
                ui->tableView->model()->setData(ui->tableView->model()->index(i,j), givestring);  //then change the cell's data with the new variable
            }
        }
    

    Problem here when index is (1,1)..and so on to the (7,3) i.e. (last element in the table)
    There is no problem when index is = (0,1), (0,2), (0,3)
    Where is my fault? Thanks.

    JonBJ 1 Reply Last reply
    0
    • H hubeytqew

      @JonB

      @JonB said in Cant change cell data one below cell:

      @hubeytqew
      So are you saying your setData()s now work OK?

      No, its not :D As I said before; I leaved this solution, I tried another approach to do table of decrypted values. I fetch values from database, decrypt it, create new table with these decrypted values. So, the table is now same with the database but values are decrypted.

      In this approach, I tried to created QTableWidget/View objects to use but I couldnt with these objects. Then I found/remembered, the solution was using ui->tableWidget->.. to show the table.

      Thank you for your patience and good answers..Appreciate it!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #22

      @hubeytqew
      I have to leave you to it. We have shown your table view/model have no problem calling setData() for simple values like text. The only difference seems to be whatever your instance1.decrypt_it() does with the encrypted string you hand it and what it returns into decrypted. I don't have your data.

      1 Reply Last reply
      1
      • H hubeytqew

        Hi, I can change data in the cell which was fetched from database but only first row. I iterate table related with the rowCount() and columnCount() first row's datas changed as expected. When I went to the second row, data's didnt changed. I guess Should I set the index etc.?
        I use QSqlTableModel with setData method

        Both getstring and givestring are QString.

            for(int i=0; i<modal->rowCount(); i++)
            {
                for(int j=1; j<modal->columnCount(); j++)
                {
                    getstring = ui->tableView->model()->data(ui->tableView->model()->index(i,j)).toString();  //get the string from table
                    givestring = instance1.do_some_work(getstring, getstring.length()/2);                //change its variable
                    ui->tableView->model()->setData(ui->tableView->model()->index(i,j), givestring);  //then change the cell's data with the new variable
                }
            }
        

        Problem here when index is (1,1)..and so on to the (7,3) i.e. (last element in the table)
        There is no problem when index is = (0,1), (0,2), (0,3)
        Where is my fault? Thanks.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #2

        @hubeytqew
        Hard to know what your issue is. Your loops go through modal variable, your body code uses ui->tableView->model(). Don't do this, it's just confusing. Do all your work on modal. Is that indeed the same thing as ui->tableView->model()??

        How do you know "data's didnt changed"? When/are you committing these setData() changes back to the database table?

        H 1 Reply Last reply
        0
        • JonBJ JonB

          @hubeytqew
          Hard to know what your issue is. Your loops go through modal variable, your body code uses ui->tableView->model(). Don't do this, it's just confusing. Do all your work on modal. Is that indeed the same thing as ui->tableView->model()??

          How do you know "data's didnt changed"? When/are you committing these setData() changes back to the database table?

          H Offline
          H Offline
          hubeytqew
          wrote on last edited by hubeytqew
          #3

          @JonB modal and model are different.

          Is that indeed the same thing as ui->tableView->model()?? Yes, it is. (If I get it right)
          modal is QSqlTableModel's object.

          I print table with this code:

              connOpen();                                          //connection opens.
              QSqlTableModel *modal;                               //this is the object.
              modal = new QSqlTableModel(this);
              modal->setTable("data");
              modal->select();
              qDebug() << modal->lastError().text();
              ui->tableView->setModel(modal);                      //here, table is printed in the tableview
          

          Actually loop is go through the table. When I want to reach table cell's data, I can reach it via code which was given above.(at the question) So I think, I can change them via same commands.

          The problem is I change variables in the cells, up to the first row. When iteration goes second row, I can not change them. Then I guess, maybe Do I need to set the index?

          Edit: I think, I clearly express myself :(

          JonBJ 1 Reply Last reply
          0
          • H hubeytqew

            @JonB modal and model are different.

            Is that indeed the same thing as ui->tableView->model()?? Yes, it is. (If I get it right)
            modal is QSqlTableModel's object.

            I print table with this code:

                connOpen();                                          //connection opens.
                QSqlTableModel *modal;                               //this is the object.
                modal = new QSqlTableModel(this);
                modal->setTable("data");
                modal->select();
                qDebug() << modal->lastError().text();
                ui->tableView->setModel(modal);                      //here, table is printed in the tableview
            

            Actually loop is go through the table. When I want to reach table cell's data, I can reach it via code which was given above.(at the question) So I think, I can change them via same commands.

            The problem is I change variables in the cells, up to the first row. When iteration goes second row, I can not change them. Then I guess, maybe Do I need to set the index?

            Edit: I think, I clearly express myself :(

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #4

            @hubeytqew
            I'm sorry but I just don't understand your statements.

            I don't know what you are up to. Having the loops go through modal as the data while the body goes through ui->tableView->model() is just plain confusing, and could be the source of your error. For all I know, modal->rowCount() might be 1 while ui->tableView->model()->rowCount() might be 8 and explain your reported behaviour.

            EDIT

            ui->tableView->setModel(modal);

            Ah, OK. So at least they are the same. I really think you should change your body code to use modal in place of ui->tableView->model(), it's a lot clearer and shorter.

            So the simple answer is: I am sorry but I do not understand enough what you are writing/saying to be able to help. Maybe somebody else will.

            H 1 Reply Last reply
            0
            • JonBJ JonB

              @hubeytqew
              I'm sorry but I just don't understand your statements.

              I don't know what you are up to. Having the loops go through modal as the data while the body goes through ui->tableView->model() is just plain confusing, and could be the source of your error. For all I know, modal->rowCount() might be 1 while ui->tableView->model()->rowCount() might be 8 and explain your reported behaviour.

              EDIT

              ui->tableView->setModel(modal);

              Ah, OK. So at least they are the same. I really think you should change your body code to use modal in place of ui->tableView->model(), it's a lot clearer and shorter.

              So the simple answer is: I am sorry but I do not understand enough what you are writing/saying to be able to help. Maybe somebody else will.

              H Offline
              H Offline
              hubeytqew
              wrote on last edited by hubeytqew
              #5

              @JonB

              void manager::on_pushButton_show_clicked()
              {
                  encrypted_decrypted instance1;
                  connOpen();
                  QSqlTableModel *modal;
                  modal = new QSqlTableModel(this);
                  modal->setTable("data");
                  modal->select();
                  qDebug() << modal->lastError().text();
                  ui->tableView->setModel(modal);
                  //up to now, table is printed in the tableview
                  for(int i=0; i<modal->rowCount(); i++)
                  {
                      for(int j=1; j<modal->columnCount(); j++)
                      {
                          encrypted = ui->tableView->model()->data(ui->tableView->model()->index(i,j)).toString();
                          decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);
                          ui->tableView->model()->setData(ui->tableView->model()->index(i,j), decrypted);
                      }
                  }
                  //up to now, cells of the *first row* of the **table** changed up to first row.
                  //because cells of the *second row* cant changed.
                  {
                      connClose();                
                      //connection closed.
                  }
              }
              

              This is the code when I press the button. This code works properly up to iteration came to the second row.
              I think model is the method of the tableView? So I can use it without error..

              EDIT: When I change with model to the modal -> QtCreator says;
              There is no member named modal() in QTableView.
              I did not any change in the base classes including QTableView.
              model() Isn't it member of the tableView ? Or should I use another method? I think this is the work properly.

              JonBJ 1 Reply Last reply
              0
              • H hubeytqew

                @JonB

                void manager::on_pushButton_show_clicked()
                {
                    encrypted_decrypted instance1;
                    connOpen();
                    QSqlTableModel *modal;
                    modal = new QSqlTableModel(this);
                    modal->setTable("data");
                    modal->select();
                    qDebug() << modal->lastError().text();
                    ui->tableView->setModel(modal);
                    //up to now, table is printed in the tableview
                    for(int i=0; i<modal->rowCount(); i++)
                    {
                        for(int j=1; j<modal->columnCount(); j++)
                        {
                            encrypted = ui->tableView->model()->data(ui->tableView->model()->index(i,j)).toString();
                            decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);
                            ui->tableView->model()->setData(ui->tableView->model()->index(i,j), decrypted);
                        }
                    }
                    //up to now, cells of the *first row* of the **table** changed up to first row.
                    //because cells of the *second row* cant changed.
                    {
                        connClose();                
                        //connection closed.
                    }
                }
                

                This is the code when I press the button. This code works properly up to iteration came to the second row.
                I think model is the method of the tableView? So I can use it without error..

                EDIT: When I change with model to the modal -> QtCreator says;
                There is no member named modal() in QTableView.
                I did not any change in the base classes including QTableView.
                model() Isn't it member of the tableView ? Or should I use another method? I think this is the work properly.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #6

                @hubeytqew

                • Put in qDebug() statements so we know that i counts up to 8, not just 1.
                • Test the result of the setData() calls for true/false.

                There is no member named modal() in QTableView.

                I never said to write modal()! Look, your loop is:

                for(int i=0; i<modal->rowCount(); i++)

                So just like I said don't you think your code would be a lot clearer if in the body of that loop you used modal->... rather than ui->tableView->model()->... everywhere? Why use different variables/expressions which refer to the same model? Otherwise reading the code we are not sure that what we are counting through is the same as what we are accessing.....

                H JonBJ 2 Replies Last reply
                0
                • JonBJ JonB

                  @hubeytqew

                  • Put in qDebug() statements so we know that i counts up to 8, not just 1.
                  • Test the result of the setData() calls for true/false.

                  There is no member named modal() in QTableView.

                  I never said to write modal()! Look, your loop is:

                  for(int i=0; i<modal->rowCount(); i++)

                  So just like I said don't you think your code would be a lot clearer if in the body of that loop you used modal->... rather than ui->tableView->model()->... everywhere? Why use different variables/expressions which refer to the same model? Otherwise reading the code we are not sure that what we are counting through is the same as what we are accessing.....

                  H Offline
                  H Offline
                  hubeytqew
                  wrote on last edited by
                  #7

                  @JonB Sorry about modal(). This is my fault.
                  Actually; one minute before I changed ui->tableView->model() to the modal....thanks to the code you wrote 2 years ago :D (in another ones question).

                  *I controlled the iteration, it goes (0,1)->(0,2)->(0,3)->(1,1)->.....->(7,3) which I want.
                  *I used to QSqlQueryModel in the code with tableView and when I run the code, table came to the screen then I think; Ok, this is the right code to deal with table. Thats why ui->tableView->model() part in the code.

                  Whatever it is :D I clear code to the:

                  void manager::on_pushButton_show_clicked()
                  {
                      encrypted_decrypted instance1;
                      connOpen();
                      QSqlTableModel *modal;
                      modal = new QSqlTableModel(this);
                      modal->setTable("data");
                      modal->select();
                      qDebug() << modal->lastError().text();
                      ui->tableView->setModel(modal);
                      //up to now, table is printed in the tableview
                      for(int i=0; i<modal->rowCount(); i++)
                      {
                          for(int j=1; j<modal->columnCount(); j++)
                          {
                              encrypted = modal->data(modal->index(i,j)).toString();
                              decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);
                              modal->setData(modal->index(i,j), decrypted);
                          }
                      }
                      //up to now, cells of the *first row* of the **table** changed up to first row.
                      //because cells of the *second row* cant changed.
                      {
                          connClose();                
                          //connection closed.
                      }
                  }
                  

                  Now, the code is clear and correct I think but the problem exists. It changes only cells of the first row.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @hubeytqew

                    • Put in qDebug() statements so we know that i counts up to 8, not just 1.
                    • Test the result of the setData() calls for true/false.

                    There is no member named modal() in QTableView.

                    I never said to write modal()! Look, your loop is:

                    for(int i=0; i<modal->rowCount(); i++)

                    So just like I said don't you think your code would be a lot clearer if in the body of that loop you used modal->... rather than ui->tableView->model()->... everywhere? Why use different variables/expressions which refer to the same model? Otherwise reading the code we are not sure that what we are counting through is the same as what we are accessing.....

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #8

                    @hubeytqew

                    @JonB said in Cant change cell data one below cell:

                    Test the result of the setData() calls for true/false.

                    H 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @hubeytqew

                      @JonB said in Cant change cell data one below cell:

                      Test the result of the setData() calls for true/false.

                      H Offline
                      H Offline
                      hubeytqew
                      wrote on last edited by hubeytqew
                      #9

                      @JonB How can I control this? I did not know how to use setData to the result of it.

                      JonBJ 1 Reply Last reply
                      0
                      • H hubeytqew

                        @JonB How can I control this? I did not know how to use setData to the result of it.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #10

                        @hubeytqew said in Cant change cell data one below cell:

                        @JonB How can I control this? I did not know how to use setData to the result of it.

                        Then please look at the documentation.....

                        H 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @hubeytqew said in Cant change cell data one below cell:

                          @JonB How can I control this? I did not know how to use setData to the result of it.

                          Then please look at the documentation.....

                          H Offline
                          H Offline
                          hubeytqew
                          wrote on last edited by hubeytqew
                          #11

                          @JonB hi again,

                          setData() returns true for the first row..(i.e. when index -> (0,1), (0,2), (0,3) )
                          when index -> (1,1), (1,2), (1,3), (2,1)...(7,3) return false.

                              for(int j=1; j<modal->columnCount(); j++)
                              {
                                  encrypted = modal->data(modal->index(i,j)).toString();
                                  decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);   //**
                                  modal->setData(modal->index(i,j), decrypted);  //**
                              }
                          

                          There is a function on the asteriks line. This function dumps the result to QString decrypted, which is used in setData(). I think maybe there is a problem here, causing it to not work properly when Qstring goes into setData(), but I'm sure there is no problem with the function. So, maybe should I set index or something similar?

                          Edit: I manually use these two lines, then qDebug() decrypted.

                                  encrypted = modal->data(modal->index(i,j)).toString();
                                  decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); 
                          

                          It goes to the second row, the third row... it goes to the end of the table (i.e. the last row) so the function is fine and it reaches the other rows except the first row (so that the indexes are ok)

                          Where could the problem originate from? Am I not using setData() correctly?

                          JonBJ 1 Reply Last reply
                          0
                          • H hubeytqew

                            @JonB hi again,

                            setData() returns true for the first row..(i.e. when index -> (0,1), (0,2), (0,3) )
                            when index -> (1,1), (1,2), (1,3), (2,1)...(7,3) return false.

                                for(int j=1; j<modal->columnCount(); j++)
                                {
                                    encrypted = modal->data(modal->index(i,j)).toString();
                                    decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);   //**
                                    modal->setData(modal->index(i,j), decrypted);  //**
                                }
                            

                            There is a function on the asteriks line. This function dumps the result to QString decrypted, which is used in setData(). I think maybe there is a problem here, causing it to not work properly when Qstring goes into setData(), but I'm sure there is no problem with the function. So, maybe should I set index or something similar?

                            Edit: I manually use these two lines, then qDebug() decrypted.

                                    encrypted = modal->data(modal->index(i,j)).toString();
                                    decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); 
                            

                            It goes to the second row, the third row... it goes to the end of the table (i.e. the last row) so the function is fine and it reaches the other rows except the first row (so that the indexes are ok)

                            Where could the problem originate from? Am I not using setData() correctly?

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #12

                            @hubeytqew said in Cant change cell data one below cell:

                            when index -> (1,1), (1,2), (1,3), (2,1)...(7,3) return false.

                            So now at least you know it is reporting it does not work on rows 1+.

                            So try this:

                                    QString s1 = modal->data(modal->index(i,j)).toString();
                                    QString s2 = s1 + "Z";
                                    bool success = modal->setData(modal->index(i,j), s2);
                                    Q_ASSERT(success);    // you will need `#include <QDebug>` if you do not already have it
                            

                            Does this work fine?

                            Assuming it does, my guess is that the value returned into decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); is somehow not acceptable to store as a QVariant? I do not see that you define either encrypted or decrypted in on_pushButton_show_clicked(), that is not right if you are using member variables here. In any case, it's over to you to find out what is going with decrypted/decrypt_it()....

                            Purely at a guess: what type are all your columns in the table. You seem to convert each value into a string, then do something to it which returns something (may or may not also be a string), and then assume whatever that type is (decrypted) can automatically be stored in any column in the table. It has to be a matching type, else how would it get stored back to the database table.... maybe if everything is a string this won't be the issue.

                            In case it is relevant: I think the default for QSqlTableModel::editStrategy() is QSqlTableModel::OnFieldChange. Try setting modal->setEditStrategy(QSqlTableModel::OnManualSubmit): does that change the behaviour?

                            H 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @hubeytqew said in Cant change cell data one below cell:

                              when index -> (1,1), (1,2), (1,3), (2,1)...(7,3) return false.

                              So now at least you know it is reporting it does not work on rows 1+.

                              So try this:

                                      QString s1 = modal->data(modal->index(i,j)).toString();
                                      QString s2 = s1 + "Z";
                                      bool success = modal->setData(modal->index(i,j), s2);
                                      Q_ASSERT(success);    // you will need `#include <QDebug>` if you do not already have it
                              

                              Does this work fine?

                              Assuming it does, my guess is that the value returned into decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); is somehow not acceptable to store as a QVariant? I do not see that you define either encrypted or decrypted in on_pushButton_show_clicked(), that is not right if you are using member variables here. In any case, it's over to you to find out what is going with decrypted/decrypt_it()....

                              Purely at a guess: what type are all your columns in the table. You seem to convert each value into a string, then do something to it which returns something (may or may not also be a string), and then assume whatever that type is (decrypted) can automatically be stored in any column in the table. It has to be a matching type, else how would it get stored back to the database table.... maybe if everything is a string this won't be the issue.

                              In case it is relevant: I think the default for QSqlTableModel::editStrategy() is QSqlTableModel::OnFieldChange. Try setting modal->setEditStrategy(QSqlTableModel::OnManualSubmit): does that change the behaviour?

                              H Offline
                              H Offline
                              hubeytqew
                              wrote on last edited by hubeytqew
                              #13

                              @JonB

                              for(int i=0; i<modal->rowCount(); i++)
                              {
                                  for(int j=1; j<modal->columnCount(); j++)
                                  {
                                      encrypted = modal->data(modal->index(i,j)).toString();
                                      decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);
                                      modal->setData(modal->index(i,j), decrypted);
                                  }
                              

                              My bad...Both encrypted and decrypted are QString. I fecth the value from the table in TableModel then assign it in QString. Then the encrypted and length of it, entries the function decrypt_it. This function creates a QString. The decrypted QString is basically the decrypted version of the encrypted QString. Then the new QString decrypted, should be the new variable of a cell value.

                              Basically, encrypted strings are stored in the database. After the encrypted strings taken from the database are decrypted, they should be displayed in the table in the application. The table in the database is first created in the application, and then I decrypt the values ​​in it one by one.(So the table should be able to be updated) However, I don't think this is the way. Therefore, in the application, I will obtain the data one by one from the database and then create a new table.

                              I haven't tried the code you wrote yet, I will try it soon.

                              Edit:

                                  QString s1 = modal->data(modal->index(i,j)).toString();
                                  QString s2 = s1 + "Z";
                                  bool success = modal->setData(modal->index(i,j), s2);
                                  Q_ASSERT(success);    // you will need `#include <QDebug>` if you do not already have it
                              
                              Does this work fine?
                              

                              Yes works fine, ASSERT: "success" printed.

                              JonBJ 1 Reply Last reply
                              0
                              • H hubeytqew

                                @JonB

                                for(int i=0; i<modal->rowCount(); i++)
                                {
                                    for(int j=1; j<modal->columnCount(); j++)
                                    {
                                        encrypted = modal->data(modal->index(i,j)).toString();
                                        decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);
                                        modal->setData(modal->index(i,j), decrypted);
                                    }
                                

                                My bad...Both encrypted and decrypted are QString. I fecth the value from the table in TableModel then assign it in QString. Then the encrypted and length of it, entries the function decrypt_it. This function creates a QString. The decrypted QString is basically the decrypted version of the encrypted QString. Then the new QString decrypted, should be the new variable of a cell value.

                                Basically, encrypted strings are stored in the database. After the encrypted strings taken from the database are decrypted, they should be displayed in the table in the application. The table in the database is first created in the application, and then I decrypt the values ​​in it one by one.(So the table should be able to be updated) However, I don't think this is the way. Therefore, in the application, I will obtain the data one by one from the database and then create a new table.

                                I haven't tried the code you wrote yet, I will try it soon.

                                Edit:

                                    QString s1 = modal->data(modal->index(i,j)).toString();
                                    QString s2 = s1 + "Z";
                                    bool success = modal->setData(modal->index(i,j), s2);
                                    Q_ASSERT(success);    // you will need `#include <QDebug>` if you do not already have it
                                
                                Does this work fine?
                                

                                Yes works fine, ASSERT: "success" printed.

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #14

                                @hubeytqew said in Cant change cell data one below cell:

                                Yes works fine, ASSERT: "success" printed.

                                This shows that we are able to set the data to a legitimate string. Your modal->index(i,j) is always valid/correct as is your call to setData(). However, I believe you will find the value you try to pass from your original decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); is not valid, hence some rejection during setData().

                                Now show me your definition of your instance1.decrypt_it() method. It claims to take a QString and return a QString from it, "encrypted". But I "bet" your code does not return a valid QString which can be stored in the database table in its char string type. Rather I would guess it returns some sequence of non-character bytes. Which you cannot store in your database's char string type. Or, it may be the other way round: decrypt_it() returns a good string of characters, but the parameter you are passing in as modal->data(modal->index(i,j)).toString() which should be an encryption of some plain text string is not/is wrong.

                                You cannot use the same column in a SQL table to hold either a decrypted, plain-text genuine string of characters (database char or varchar or similar) or also an encrypted sequence of binary bytes (database blob or similar)....

                                H 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @hubeytqew said in Cant change cell data one below cell:

                                  Yes works fine, ASSERT: "success" printed.

                                  This shows that we are able to set the data to a legitimate string. Your modal->index(i,j) is always valid/correct as is your call to setData(). However, I believe you will find the value you try to pass from your original decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2); is not valid, hence some rejection during setData().

                                  Now show me your definition of your instance1.decrypt_it() method. It claims to take a QString and return a QString from it, "encrypted". But I "bet" your code does not return a valid QString which can be stored in the database table in its char string type. Rather I would guess it returns some sequence of non-character bytes. Which you cannot store in your database's char string type. Or, it may be the other way round: decrypt_it() returns a good string of characters, but the parameter you are passing in as modal->data(modal->index(i,j)).toString() which should be an encryption of some plain text string is not/is wrong.

                                  You cannot use the same column in a SQL table to hold either a decrypted, plain-text genuine string of characters (database char or varchar or similar) or also an encrypted sequence of binary bytes (database blob or similar)....

                                  H Offline
                                  H Offline
                                  hubeytqew
                                  wrote on last edited by
                                  #15

                                  @JonB

                                  Here is the decrypt_it() function: the function decrypt taken from the internet which is an openssl cryptographic function uses AES.

                                  QString decrypt_it(QString crypt_password, int cipher_len)
                                     {
                                         QString decrypted_string;
                                         unsigned char decrypted[64];
                                         unsigned char cipher[64];
                                         for(int i=0;i<cipher_len;++i)
                                         {
                                              QString hexString = crypt_password.mid(i*2,2);
                                              bool ok = false;
                                              cipher[i] = (unsigned char)hexString.toUShort(&ok,16);  //if not ok, handle error
                                         }
                                         /
                                         int dec_len = decrypt(cipher, cipher_len, key, decrypted);
                                         for(int i=0; i<dec_len; i++)
                                         {
                                             decrypted_string += decrypted[i];
                                         }
                                         return decrypted_string;
                                     }
                                  

                                  It takes QString and returns a QString again. As I said before; I tried manually, the function decrypt_it() takes and returns QStrings without a problem accros the entire table. I do not know why but when I went to the second row, it did not run as expected -.-

                                  The encrypted values are hex values, they stored in the sql table without a problem(They can shown w/o problem). By the way, I do not want change to variable inside the database. I should only change the variable/data in the table which is in application(qt).

                                  JonBJ 1 Reply Last reply
                                  0
                                  • H hubeytqew

                                    @JonB

                                    Here is the decrypt_it() function: the function decrypt taken from the internet which is an openssl cryptographic function uses AES.

                                    QString decrypt_it(QString crypt_password, int cipher_len)
                                       {
                                           QString decrypted_string;
                                           unsigned char decrypted[64];
                                           unsigned char cipher[64];
                                           for(int i=0;i<cipher_len;++i)
                                           {
                                                QString hexString = crypt_password.mid(i*2,2);
                                                bool ok = false;
                                                cipher[i] = (unsigned char)hexString.toUShort(&ok,16);  //if not ok, handle error
                                           }
                                           /
                                           int dec_len = decrypt(cipher, cipher_len, key, decrypted);
                                           for(int i=0; i<dec_len; i++)
                                           {
                                               decrypted_string += decrypted[i];
                                           }
                                           return decrypted_string;
                                       }
                                    

                                    It takes QString and returns a QString again. As I said before; I tried manually, the function decrypt_it() takes and returns QStrings without a problem accros the entire table. I do not know why but when I went to the second row, it did not run as expected -.-

                                    The encrypted values are hex values, they stored in the sql table without a problem(They can shown w/o problem). By the way, I do not want change to variable inside the database. I should only change the variable/data in the table which is in application(qt).

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #16

                                    @hubeytqew said in Cant change cell data one below cell:

                                    cipher[i] = (unsigned char)hexString.toUShort(&ok,16); //if not ok, handle error

                                    So if you are debugging a problem in your code you are supposed to do something about that comment, not ignore it! At minimum a Q_ASSERT(ok).

                                    This does not (guarantee to) return a legitimate string acceptable to the database. It returns a sequence of bytes. For example, maybe the crypt_password contains 00 or FF, either of which may return a character which cannot be stored in the database's char type.

                                    You might also try one thing: change your "row loop" to start from 1 instead of 0. Does row #1 now succeed or fail? That tells you whether setData() has trouble with the data from row #1 but not from row #0, as opposed to it being anything about "the second row setData() is called on".

                                    In any case, you say you tried my example above which just adds a Z to the incoming string and stores it back, and you said that worked fine. Across all rows and columns. The only difference is that your code "decrypts" the incoming hex string and generates some string from that, all via decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);, and then it fails. So only you have your data to discover why that is. That means it is over to you now....

                                    H 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @hubeytqew said in Cant change cell data one below cell:

                                      cipher[i] = (unsigned char)hexString.toUShort(&ok,16); //if not ok, handle error

                                      So if you are debugging a problem in your code you are supposed to do something about that comment, not ignore it! At minimum a Q_ASSERT(ok).

                                      This does not (guarantee to) return a legitimate string acceptable to the database. It returns a sequence of bytes. For example, maybe the crypt_password contains 00 or FF, either of which may return a character which cannot be stored in the database's char type.

                                      You might also try one thing: change your "row loop" to start from 1 instead of 0. Does row #1 now succeed or fail? That tells you whether setData() has trouble with the data from row #1 but not from row #0, as opposed to it being anything about "the second row setData() is called on".

                                      In any case, you say you tried my example above which just adds a Z to the incoming string and stores it back, and you said that worked fine. Across all rows and columns. The only difference is that your code "decrypts" the incoming hex string and generates some string from that, all via decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);, and then it fails. So only you have your data to discover why that is. That means it is over to you now....

                                      H Offline
                                      H Offline
                                      hubeytqew
                                      wrote on last edited by hubeytqew
                                      #17

                                      @JonB

                                      This does not (guarantee to) return a legitimate string acceptable to the database. It returns a sequence of bytes. For example, maybe the crypt_password contains 00 or FF, either of which may return a character which cannot be stored in the database's char type.

                                      Actually, I tried this. Previously, when I encrypted the password I had written, I was seeing strange characters. I could see this in the database but wasn't sure if it was converting characters correctly. Then I decided to convert it to hex. That way, only hex would show up in the database, which is what it was. So, I'm pretty sure I can see and save the hexes correctly in the database. I'm not sending anything other than the character type of the database; only hex values between 00 and FF.

                                      So the data I get from the database contains only hex. Between 00 and FF.

                                      You might also try one thing: change your "row loop" to start from 1 instead of 0. Does row #1 now succeed or fail? That tells you whether setData() has trouble with the data from row #1 but not from row #0, as opposed to it being anything about "the second row setData() is called on".

                                      I had tried it either. Whether I start from 0 or 6. I have a problem when it goes to the next line. It can add the data in each row to the list. (Separately) But when it goes to the next line, the problem starts :D

                                      In any case, you say you tried my example above which just adds a Z to the incoming string and stores it back, and you said that worked fine. Across all rows and columns. The only difference is that your code "decrypts" the incoming hex string and generates some string from that, all via decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);, and then it fails. So only you have your data to discover why that is. That means it is over to you now....

                                      Yes like that. I got the correct result. I couldn't find a solution to the problem either. So, now I'll pull the data from the database one by one, decrypt it, then create a new list in the application and add it to it. I had this idea in mind from the very beginning but this solution I was trying to do was easier :D
                                      Anyway, thank you very much for your efforts.

                                      Edit: I have a question. As I said before, I want to show these values in table. I have a tableview in ui. For example, if I want to use QTableWidget I place and print these values in cells but I can't integrate QTableWidget or QTableWidgetItem or whatever into the ui.
                                      ui->tableView->setModel(tablewidgetem);

                                      If I only use QTableWidget I can't set the visual. Prints a table at the top left of the dialog box.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • H hubeytqew

                                        @JonB

                                        This does not (guarantee to) return a legitimate string acceptable to the database. It returns a sequence of bytes. For example, maybe the crypt_password contains 00 or FF, either of which may return a character which cannot be stored in the database's char type.

                                        Actually, I tried this. Previously, when I encrypted the password I had written, I was seeing strange characters. I could see this in the database but wasn't sure if it was converting characters correctly. Then I decided to convert it to hex. That way, only hex would show up in the database, which is what it was. So, I'm pretty sure I can see and save the hexes correctly in the database. I'm not sending anything other than the character type of the database; only hex values between 00 and FF.

                                        So the data I get from the database contains only hex. Between 00 and FF.

                                        You might also try one thing: change your "row loop" to start from 1 instead of 0. Does row #1 now succeed or fail? That tells you whether setData() has trouble with the data from row #1 but not from row #0, as opposed to it being anything about "the second row setData() is called on".

                                        I had tried it either. Whether I start from 0 or 6. I have a problem when it goes to the next line. It can add the data in each row to the list. (Separately) But when it goes to the next line, the problem starts :D

                                        In any case, you say you tried my example above which just adds a Z to the incoming string and stores it back, and you said that worked fine. Across all rows and columns. The only difference is that your code "decrypts" the incoming hex string and generates some string from that, all via decrypted = instance1.decrypt_it(encrypted, encrypted.length()/2);, and then it fails. So only you have your data to discover why that is. That means it is over to you now....

                                        Yes like that. I got the correct result. I couldn't find a solution to the problem either. So, now I'll pull the data from the database one by one, decrypt it, then create a new list in the application and add it to it. I had this idea in mind from the very beginning but this solution I was trying to do was easier :D
                                        Anyway, thank you very much for your efforts.

                                        Edit: I have a question. As I said before, I want to show these values in table. I have a tableview in ui. For example, if I want to use QTableWidget I place and print these values in cells but I can't integrate QTableWidget or QTableWidgetItem or whatever into the ui.
                                        ui->tableView->setModel(tablewidgetem);

                                        If I only use QTableWidget I can't set the visual. Prints a table at the top left of the dialog box.

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by JonB
                                        #18

                                        @hubeytqew said in Cant change cell data one below cell:

                                        But when it goes to the next line, the problem starts :D

                                        Let's assume that is indeed the case. I don't know why. (Other than: please bear in mind, if you set legitimate strings, it all works, but not from your data, right? So there must be something about your data which is causing the problem.)

                                        1. What does qDebug() << modal->editStrategy() return?

                                        2. Can you also add a slot so that we know what is going on:

                                        connect(modal, &QSqlTableModel::beforeUpdate, [](int row, QSqlRecord &record) { qDebug() << "Updating row" << row; });
                                        

                                        Does the slot get called?

                                        1. Immediately after the bool success = modal->setData(modal->index(i,j), s2); put in a
                                        if (!success)
                                            qDebug() << modal->lastError().text();
                                        

                                        I don't know whether this will tell us anything, but it might do.

                                        That is 3 separate question for you to report back on!

                                        H 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @hubeytqew said in Cant change cell data one below cell:

                                          But when it goes to the next line, the problem starts :D

                                          Let's assume that is indeed the case. I don't know why. (Other than: please bear in mind, if you set legitimate strings, it all works, but not from your data, right? So there must be something about your data which is causing the problem.)

                                          1. What does qDebug() << modal->editStrategy() return?

                                          2. Can you also add a slot so that we know what is going on:

                                          connect(modal, &QSqlTableModel::beforeUpdate, [](int row, QSqlRecord &record) { qDebug() << "Updating row" << row; });
                                          

                                          Does the slot get called?

                                          1. Immediately after the bool success = modal->setData(modal->index(i,j), s2); put in a
                                          if (!success)
                                              qDebug() << modal->lastError().text();
                                          

                                          I don't know whether this will tell us anything, but it might do.

                                          That is 3 separate question for you to report back on!

                                          H Offline
                                          H Offline
                                          hubeytqew
                                          wrote on last edited by hubeytqew
                                          #19

                                          @JonB

                                          Let's assume that is indeed the case. I don't know why. (Other than: please bear in mind, if you set legitimate strings, it all works, but not from your data, right? So there must be something about your data which is causing the problem.)

                                          I'm sure the returned QString is legitimate. I can fetch data from database for all rows, but seperately. Problem comes in when I do this in a loop. I can do any line, regardless of the order of the line. No matter which row, the data in the list does not change when the loop moves to the next row.

                                          1. What does qDebug() << modal->editStrategy() return?

                                          It returns 1 Even in the loop or out the loop.

                                          Can you also add a slot so that we know what is going on:
                                          connect(modal, &QSqlTableModel::beforeUpdate, [](int row, QSqlRecord &record) { qDebug() << "Updating row" << row; });
                                          Does the slot get called?

                                          Slot called but only print "" 24 times. There is no Updating row output even for the first line.

                                          Immediately after the bool success = modal->setData(modal->index(i,j), s2); put in a
                                          if (!success)
                                          qDebug() << modal->lastError().text();

                                          It prints 21 times "" .. I have 24 value in database 8x3. First 3value (first row) are OK. When the next row comes in, it returns empty. The value of success is false for the remain 21 value..

                                          Again; for the first row success has true, in remain 7 row return false but there is no lastError output..

                                          Only the reason is my function I guess, but it is not possible to my tests.
                                          Whatever, I am trying another solution. I edited my last post but I think you didnt see it. Can you give me idea about it?

                                          Edit: I had a question but I solved it. Now, I printed data of cell of table into the tablewidget in the ui. I confused about QTableWidget and QTableView. I tried to create pointer of that and used it but I couldnt. Then I finally found; I should use ui->tableWidget->... So now, my program works as expected.
                                          What is the difference between object of QTableWidget/QTableView and only ui->tableWidget ? In many posts, people used objects of widget/view. Should I use too? I dont get it right.

                                          JonBJ 1 Reply Last reply
                                          0
                                          • H hubeytqew

                                            @JonB

                                            Let's assume that is indeed the case. I don't know why. (Other than: please bear in mind, if you set legitimate strings, it all works, but not from your data, right? So there must be something about your data which is causing the problem.)

                                            I'm sure the returned QString is legitimate. I can fetch data from database for all rows, but seperately. Problem comes in when I do this in a loop. I can do any line, regardless of the order of the line. No matter which row, the data in the list does not change when the loop moves to the next row.

                                            1. What does qDebug() << modal->editStrategy() return?

                                            It returns 1 Even in the loop or out the loop.

                                            Can you also add a slot so that we know what is going on:
                                            connect(modal, &QSqlTableModel::beforeUpdate, [](int row, QSqlRecord &record) { qDebug() << "Updating row" << row; });
                                            Does the slot get called?

                                            Slot called but only print "" 24 times. There is no Updating row output even for the first line.

                                            Immediately after the bool success = modal->setData(modal->index(i,j), s2); put in a
                                            if (!success)
                                            qDebug() << modal->lastError().text();

                                            It prints 21 times "" .. I have 24 value in database 8x3. First 3value (first row) are OK. When the next row comes in, it returns empty. The value of success is false for the remain 21 value..

                                            Again; for the first row success has true, in remain 7 row return false but there is no lastError output..

                                            Only the reason is my function I guess, but it is not possible to my tests.
                                            Whatever, I am trying another solution. I edited my last post but I think you didnt see it. Can you give me idea about it?

                                            Edit: I had a question but I solved it. Now, I printed data of cell of table into the tablewidget in the ui. I confused about QTableWidget and QTableView. I tried to create pointer of that and used it but I couldnt. Then I finally found; I should use ui->tableWidget->... So now, my program works as expected.
                                            What is the difference between object of QTableWidget/QTableView and only ui->tableWidget ? In many posts, people used objects of widget/view. Should I use too? I dont get it right.

                                            JonBJ Online
                                            JonBJ Online
                                            JonB
                                            wrote on last edited by
                                            #20

                                            @hubeytqew
                                            So are you saying your setData()s now work OK?

                                            What is the difference between object of QTableWidget/QTableView and only ui->tableWidget ?

                                            QTableView provides the base functionality for binding to a data model and showing/editing its content in rows and columns of cells.

                                            QTableWidget is derived from QTableView, so it starts out with all that class's functionality. Principally it then adds it its own data model, so you don't have to create one, and it also adds an "item based approach" to adding data.

                                            QTableWidget is easiest for beginners as it is self-contained, you don't have to understand about models to use it. But as soon as you have/want your own model, or want to use a SQL database for the model, you want QTableView.

                                            Your ui->tableWidget is presumably a QTableWidget you have created in Designer.

                                            H 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