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 update column B of a QSqlRelationalTableModel after the update of column A?
Forum Updated to NodeBB v4.3 + New Features

How to update column B of a QSqlRelationalTableModel after the update of column A?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by
    #1

    I have two columns in a model (QSqlRelationalTableModel).
    I want after the update of column A (in a QTableView), another column, column B, to be automatically updated.
    How can I do this?
    I suppose I must subclass the QSqlRelationalTableModel?
    Any info is welcome.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Or do it in your db directly with an update trigger.

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #3

        The problem is that with the trigger (I tried it) I cannot make the text in the second column have lower case (non latin characters) (it's an SQLite db, does not support lower case for non latin).
        (I 've already seen this)

        JonBJ 1 Reply Last reply
        0
        • P Panoss

          The problem is that with the trigger (I tried it) I cannot make the text in the second column have lower case (non latin characters) (it's an SQLite db, does not support lower case for non latin).
          (I 've already seen this)

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

          @Panoss
          Then in your setData() override update column B whenever you see column A being updated.

          P 1 Reply Last reply
          0
          • JonBJ JonB

            @Panoss
            Then in your setData() override update column B whenever you see column A being updated.

            P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #5

            @JonB said in How to update column B of a QSqlRelationalTableModel after the update of column A?:

            @Panoss
            Then in your setData() override update column B whenever you see column A being updated.

            This is what I 've done so far:

            bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){
                int nameIndex = 1;  // index of field 'name'
                if(index.column() == nameIndex){
                    ///index.model(). ?????
                }
            
                return QSqlTableModel::setData(index, value, role);
            }
            

            So how do I update column 3?

            P 1 Reply Last reply
            0
            • P Panoss

              @JonB said in How to update column B of a QSqlRelationalTableModel after the update of column A?:

              @Panoss
              Then in your setData() override update column B whenever you see column A being updated.

              This is what I 've done so far:

              bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){
                  int nameIndex = 1;  // index of field 'name'
                  if(index.column() == nameIndex){
                      ///index.model(). ?????
                  }
              
                  return QSqlTableModel::setData(index, value, role);
              }
              

              So how do I update column 3?

              P Offline
              P Offline
              Panoss
              wrote on last edited by Panoss
              #6

              This is what I 've done and seems to work:

              bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){  
                  int nameColumnIndex = 1;
                  int name_lowerColumnIndex = 3;
                  if(index.column() == nameColumnIndex){
                      int row = index.row();
                      QModelIndex name_lower_index = index.model()->index(row,name_lowerColumnIndex);
                      QString v = value.toString();
                      ArticlesModel::setData(name_lower_index, v.toLower(), Qt::EditRole);
                  }
                  return QSqlTableModel::setData(index, value, role);
              }
              

              Is it ok?

              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