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. Change cell data in tableview QT
Forum Updated to NodeBB v4.3 + New Features

Change cell data in tableview QT

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 459 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

    I am trying to pull data's in database then print on tableview then change them one by one. I can reach each of the cells (in later times I'll iterate it via for whatever..) but I can't change them. This is the code when I press the button, it shows database data's without a problem but I can't change them. How can I achieve this?

    void manager::on_pushButton_show_clicked()                      
    {
        connOpen();
        QSqlQueryModel * modal = new QSqlQueryModel();
        QSqlQuery* qry = new QSqlQuery(mydb);
        qry->prepare("select * from data");
        qry->exec();
        modal->setQuery(*qry);
        ui->tableView->setModel(modal);      //up to now, there is no problem, table appears as expected..
        QString cellstring = ui->tableView->model()->data(ui->tableView->model()->index(0,2)).toString();
        qDebug() << cellstring;              //I can read the cell's data, *cellstring* holding it
        QString newstring = "hello";
    *1  ui->tableView->model()->setData(ui->tableView->model()->index(0,2), newstring);
    *2  QString another = ui->tableView->model()->data(ui->tableView->model()->index(0,2)).toString();
        qDebug() << another;
    

    in number1 line I try to change the data inside the cell.
    in number2 line I read the cell's data then print it, but there is no change at all.
    I still see the old variable;

    It prints: abcdefgh
    It should print: hello

    Is the setData method does not work properly? Any suggestions? Thanks.
    I am newbie here, I wish I clearly express myself..

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      From the document of QSqlQueryModel:

      The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

      H 2 Replies Last reply
      2
      • B Bonnie

        From the document of QSqlQueryModel:

        The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

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

        @Bonnie Is there an example for this? I did not implement subclass. Thx.

        Edit: I want to fetch database then do some calculations on it then print it via table. If I use QSqlTableModel original database variables will change? It should not be change.

        Edit2: Yes it was. I changed the table in the app, and there is no changing in database. Thx Bonnie..

        B 1 Reply Last reply
        0
        • B Bonnie

          From the document of QSqlQueryModel:

          The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

          H Offline
          H Offline
          hubeytqew
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • H hubeytqew

            @Bonnie Is there an example for this? I did not implement subclass. Thx.

            Edit: I want to fetch database then do some calculations on it then print it via table. If I use QSqlTableModel original database variables will change? It should not be change.

            Edit2: Yes it was. I changed the table in the app, and there is no changing in database. Thx Bonnie..

            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            @hubeytqew Well, actually it can change the database, and the behaviour is depending on the QSqlTableModel::editStrategy().
            If you don't want original database changed, make sure to set the edit strategy to QSqlTableModel::OnManualSubmit.
            Then it will only try to modify the database when you call QSqlTableModel::submitAll().
            You really need to look through the documents before asking questions :)

            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