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. TableView doesnt' change cell text?
Qt 6.11 is out! See what's new in the release blog

TableView doesnt' change cell text?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 2.8k Views 1 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.
  • devhobbyD Offline
    devhobbyD Offline
    devhobby
    wrote on last edited by
    #1

    I created my own model deriving from QSqlQueryModel

    This is flags()

    Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
    {
        if(MainWindow::isConstColumn(index.column()))
            return QAbstractItemModel::flags(index) & (~Qt::ItemIsEditable);
    
        else
            return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }
    

    This is setData()

    bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
    {
        if (index.isValid() && role == Qt::EditRole)
        {
            qDebug() << "data changed!";
    
            emit dataChanged(index, index);
            return true;
        }
    
        return false;
    }
    

    This is the signal that I use on the TableView

    connect(ui->tableView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(onTableClicked(const QModelIndex&)));
    

    I can successfully doubleClick the cells and insert new text in it

    The problem is that when I press enter, the text I entered is not applied... the original text is displayed instead.

    How can I apply the change?

    Thanks in advance.

    raven-worxR 1 Reply Last reply
    0
    • devhobbyD devhobby

      I created my own model deriving from QSqlQueryModel

      This is flags()

      Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
      {
          if(MainWindow::isConstColumn(index.column()))
              return QAbstractItemModel::flags(index) & (~Qt::ItemIsEditable);
      
          else
              return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
      }
      

      This is setData()

      bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
      {
          if (index.isValid() && role == Qt::EditRole)
          {
              qDebug() << "data changed!";
      
              emit dataChanged(index, index);
              return true;
          }
      
          return false;
      }
      

      This is the signal that I use on the TableView

      connect(ui->tableView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(onTableClicked(const QModelIndex&)));
      

      I can successfully doubleClick the cells and insert new text in it

      The problem is that when I press enter, the text I entered is not applied... the original text is displayed instead.

      How can I apply the change?

      Thanks in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @devhobby
      is this the whole implementation of your setData() method? If yes, it should be obvious why no data is actually changed.

      Did you also consider using QSqlTableModel instead of QSqlQueryModel?
      It already implements the setData() method.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      devhobbyD 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @devhobby
        is this the whole implementation of your setData() method? If yes, it should be obvious why no data is actually changed.

        Did you also consider using QSqlTableModel instead of QSqlQueryModel?
        It already implements the setData() method.

        devhobbyD Offline
        devhobbyD Offline
        devhobby
        wrote on last edited by
        #3

        @raven-worx said in TableView doesnt' change cell text?:

        @devhobby
        is this the whole implementation of your setData() method? If yes, it should be obvious why no data is actually changed.

        Yes. What is missing for making the data visually change?

        raven-worxR 1 Reply Last reply
        0
        • devhobbyD devhobby

          @raven-worx said in TableView doesnt' change cell text?:

          @devhobby
          is this the whole implementation of your setData() method? If yes, it should be obvious why no data is actually changed.

          Yes. What is missing for making the data visually change?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @devhobby
          you need to set the value back to the database (using a query). But anyway i don't know/think that you can update any currently cached data in the model.
          You can try to get the sql-record using QSqlQueryModel::record() and update it's corresponding value, afterwards call dataChanged (like you already do). But i think you don't change the data inside the model with this way.

          But again, why don't you just use a QSqlTableModel which already has read/write support implemented?!

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          devhobbyD 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @devhobby
            you need to set the value back to the database (using a query). But anyway i don't know/think that you can update any currently cached data in the model.
            You can try to get the sql-record using QSqlQueryModel::record() and update it's corresponding value, afterwards call dataChanged (like you already do). But i think you don't change the data inside the model with this way.

            But again, why don't you just use a QSqlTableModel which already has read/write support implemented?!

            devhobbyD Offline
            devhobbyD Offline
            devhobby
            wrote on last edited by
            #5

            @raven-worx said in TableView doesnt' change cell text?:

            @devhobby
            you need to set the value back to the database (using a query).

            I did it using QSqlQuery

            You can try to get the sql-record using QSqlQueryModel::record() and update it's corresponding value, afterwards call dataChanged (like you already do). But i think you don't change the data inside the model with this way.

            After sending the query, the database gets updated but the Table View still displays the old information.

            QSqlQueryModel::record(index.row()).setValue("Name", value)
            

            does nothing.

            Even by typing "name" instead of "Name"

            0_1518619232628_ad103395-90e4-4b21-9106-1bfeef8637fa-image.png

            raven-worxR 1 Reply Last reply
            0
            • devhobbyD devhobby

              @raven-worx said in TableView doesnt' change cell text?:

              @devhobby
              you need to set the value back to the database (using a query).

              I did it using QSqlQuery

              You can try to get the sql-record using QSqlQueryModel::record() and update it's corresponding value, afterwards call dataChanged (like you already do). But i think you don't change the data inside the model with this way.

              After sending the query, the database gets updated but the Table View still displays the old information.

              QSqlQueryModel::record(index.row()).setValue("Name", value)
              

              does nothing.

              Even by typing "name" instead of "Name"

              0_1518619232628_ad103395-90e4-4b21-9106-1bfeef8637fa-image.png

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @devhobby said in TableView doesnt' change cell text?:

              does nothing.

              just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).

              But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              devhobbyD 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @devhobby said in TableView doesnt' change cell text?:

                does nothing.

                just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).

                But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.

                devhobbyD Offline
                devhobbyD Offline
                devhobby
                wrote on last edited by
                #7

                @raven-worx said in TableView doesnt' change cell text?:

                @devhobby said in TableView doesnt' change cell text?:

                does nothing.

                just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).

                Is it really the only way?

                From Qt Documentation

                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

                It says to reimplement setData() but I did... i'm just missing the part where the data is changed inside the Table

                Also, it says ANOTHER option is to use QSqlTableModel

                But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.

                I prefer QSqlQueryModel because I use the method setQuery() which is protected inside QSqlTableModel.

                The latter only wants a table

                raven-worxR 1 Reply Last reply
                0
                • devhobbyD devhobby

                  @raven-worx said in TableView doesnt' change cell text?:

                  @devhobby said in TableView doesnt' change cell text?:

                  does nothing.

                  just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).

                  Is it really the only way?

                  From Qt Documentation

                  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

                  It says to reimplement setData() but I did... i'm just missing the part where the data is changed inside the Table

                  Also, it says ANOTHER option is to use QSqlTableModel

                  But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.

                  I prefer QSqlQueryModel because I use the method setQuery() which is protected inside QSqlTableModel.

                  The latter only wants a table

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @devhobby
                  see yourself. The data() method just returns the data directly from the QSqlQuery.

                  a workaround could be

                  1. cache the modified data (passed to setData()) - but still update the value to the DB
                  2. override data() and if there is cached data return it, otherwise simply return the base-class implementation
                  3. on a model reset clear the cached data again

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  devhobbyD 1 Reply Last reply
                  1
                  • raven-worxR raven-worx

                    @devhobby
                    see yourself. The data() method just returns the data directly from the QSqlQuery.

                    a workaround could be

                    1. cache the modified data (passed to setData()) - but still update the value to the DB
                    2. override data() and if there is cached data return it, otherwise simply return the base-class implementation
                    3. on a model reset clear the cached data again
                    devhobbyD Offline
                    devhobbyD Offline
                    devhobby
                    wrote on last edited by
                    #9

                    @raven-worx said in TableView doesnt' change cell text?:

                    @devhobby
                    see yourself. The data() method just returns the data directly from the QSqlQuery.

                    a workaround could be

                    1. cache the modified data (passed to setData()) - but still update the value to the DB
                    2. override data() and if there is cached data return it, otherwise simply return the base-class implementation
                    3. on a model reset clear the cached data again

                    One thing I forgot to tell you.

                    The data() function grabs the item and the role.

                    If the role is QtBackground color the cell Blue if it has been edited

                    QVariant MyModel::data(const QModelIndex& item, int role) const
                    {
                        if (!item.isValid())
                            return QVariant();
                    
                        if(role == Qt::BackgroundRole)
                        {
                            if(MainWindow::cellsEdited.contains(item))
                                return QColor(66, 197, 244, 150);
                        }
                    
                        return QSqlQueryModel::data(item, role);
                    }
                    

                    If not, return the base class data()

                    Is there something I need to do here?

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @devhobby said in TableView doesnt' change cell text?:

                      MainWindow::cellsEdited.contains(item)

                      What's this?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      devhobbyD 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @devhobby said in TableView doesnt' change cell text?:

                        MainWindow::cellsEdited.contains(item)

                        What's this?

                        devhobbyD Offline
                        devhobbyD Offline
                        devhobby
                        wrote on last edited by
                        #11

                        @VRonin said in TableView doesnt' change cell text?:

                        @devhobby said in TableView doesnt' change cell text?:

                        MainWindow::cellsEdited.contains(item)

                        What's this?

                        The slot onTableClicked() gets called when a cell of the Table View gets edited.

                        When that happens, I insert in the cellsEdited array the cell (which is a QModelIndex)

                        In data() I check if the cell is one of the edited ones. If yes, color that cell with Blue

                        1 Reply Last reply
                        0
                        • devhobbyD Offline
                          devhobbyD Offline
                          devhobby
                          wrote on last edited by
                          #12

                          Any idea?

                          I saw in the QSqlTableModel source code that the method setData() calls QSqlTableModelPrivate::setGeneratedValue to set the field in the table view.

                          QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                          raven-worxR 1 Reply Last reply
                          0
                          • devhobbyD devhobby

                            Any idea?

                            I saw in the QSqlTableModel source code that the method setData() calls QSqlTableModelPrivate::setGeneratedValue to set the field in the table view.

                            QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #13

                            @devhobby said in TableView doesnt' change cell text?:

                            QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                            you need to return your cached data in the data() method for the Qt::DisplayRole
                            Also you shouldn't use QModelIndex for persiting purposes. Use QPersistentModelIndex for such cases.

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            devhobbyD 1 Reply Last reply
                            1
                            • raven-worxR raven-worx

                              @devhobby said in TableView doesnt' change cell text?:

                              QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                              you need to return your cached data in the data() method for the Qt::DisplayRole
                              Also you shouldn't use QModelIndex for persiting purposes. Use QPersistentModelIndex for such cases.

                              devhobbyD Offline
                              devhobbyD Offline
                              devhobby
                              wrote on last edited by
                              #14

                              @raven-worx said in TableView doesnt' change cell text?:

                              @devhobby said in TableView doesnt' change cell text?:

                              QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                              you need to return your cached data in the data() method for the Qt::DisplayRole

                              I tried and worked! But tell me if I used the right approach

                              I used a QMap<QModelIndex, QVariant> to assign each index its new data

                              When data() is called, if the role is DisplayRole and there is cached data for the index, return its variant

                              Also you shouldn't use QModelIndex for persiting purposes. Use QPersistentModelIndex for such cases.

                              Could you explain this point a little more? Also, the overriding functions require a QModelIndex

                              raven-worxR 1 Reply Last reply
                              0
                              • devhobbyD devhobby

                                @raven-worx said in TableView doesnt' change cell text?:

                                @devhobby said in TableView doesnt' change cell text?:

                                QSqlQueryModel has no such method. But then, how the hell do I change the field text?

                                you need to return your cached data in the data() method for the Qt::DisplayRole

                                I tried and worked! But tell me if I used the right approach

                                I used a QMap<QModelIndex, QVariant> to assign each index its new data

                                When data() is called, if the role is DisplayRole and there is cached data for the index, return its variant

                                Also you shouldn't use QModelIndex for persiting purposes. Use QPersistentModelIndex for such cases.

                                Could you explain this point a little more? Also, the overriding functions require a QModelIndex

                                raven-worxR Offline
                                raven-worxR Offline
                                raven-worx
                                Moderators
                                wrote on last edited by
                                #15

                                @devhobby said in TableView doesnt' change cell text?:

                                Could you explain this point a little more? Also, the overriding functions require a QModelIndex

                                Just replace QMap<QModelIndex, QVariant> with QMap<QPersistentModelIndex, QVariant>.
                                You can use a QPersistentModelIndex everywhere where a QModelIndex is expcted - no conversion needed.

                                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                If you have a question please use the forum so others can benefit from the solution in the future

                                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