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. Data in a single column changes -->execute slot
Qt 6.11 is out! See what's new in the release blog

Data in a single column changes -->execute slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 3 Posters 4.4k 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.
  • M Offline
    M Offline
    Mr. Kibu
    wrote on last edited by
    #1

    Hi!

    Changing data in a QTableView, I can execute a slot like this:

    connect(ui->tblCc->model(), SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCcDataChanged()));
    

    This works fine!

    But how do I change the code above to execute the slot "onCcDataChange()" when values in only a single column of the QTableView are changed?

    Thank you!

    Franz

    raven-worxR 1 Reply Last reply
    0
    • M Mr. Kibu

      Hi!

      Changing data in a QTableView, I can execute a slot like this:

      connect(ui->tblCc->model(), SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCcDataChanged()));
      

      This works fine!

      But how do I change the code above to execute the slot "onCcDataChange()" when values in only a single column of the QTableView are changed?

      Thank you!

      Franz

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

      @Mr.-Kibu
      the dataChanged() signal already comes with the QModelIndex which has changed.
      Simply check this model index in your slot.

      connect(ui->tblCc->model(), SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCcDataChanged(const QModelIndex&));
      ...
      void onCcDataChanged(const QModelIndex& index)
      {
           if( index.column() != DESIRED_COLUMN )
               return;
          //...
      }
      

      --- 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
      • M Offline
        M Offline
        Mr. Kibu
        wrote on last edited by
        #3

        Thank you for your quick answer! I changed the code like this:

            connect(ui->tblCc->model(), SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCcDataChanged(const QModelIndex& index)));
        

        and

        void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
        {
            ui->change->setText("ccc");
        
        
            if(index.column() == 8){
                ui->change->setText("xxx");
            }
        }
        

        But I don't get either "ccc" or a "xxx" in my QLabel (change).

        I also get the error "QObject::connect: No such signal QSqlRelationalTableModel::dataChanged(const QModelIndex& index)".

        What I am doing wrong?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          remove the parameter name from the slot in the connect-statement.

          --- 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
          • M Offline
            M Offline
            Mr. Kibu
            wrote on last edited by
            #5

            Nothing changes. I do:

            connect(ui->tblCc->model(), SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCcDataChanged()));
            

            and get the error:
            "QObject::connect: No such slot BVoucherPosForm::onCcDataChanged()"

            1 Reply Last reply
            0
            • ValentinMicheletV Offline
              ValentinMicheletV Offline
              ValentinMichelet
              wrote on last edited by ValentinMichelet
              #6

              If you use Qt5, you can use the new syntax:

              connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue);
              

              Which here would be:

              connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
              

              This syntax has the advantage to be evaluated at compile time, giving you an explicit error, rather than a silently output at run time.

              raven-worxR 1 Reply Last reply
              0
              • ValentinMicheletV ValentinMichelet

                If you use Qt5, you can use the new syntax:

                connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue);
                

                Which here would be:

                connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
                

                This syntax has the advantage to be evaluated at compile time, giving you an explicit error, rather than a silently output at run time.

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

                @ValentinMichelet said:

                If you use Qt5, you can use the new syntax:

                just to add: you also need a C++11 supported compiler.

                --- 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

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

                  @ValentinMichelet said:

                  If you use Qt5, you can use the new syntax:

                  just to add: you also need a C++11 supported compiler.

                  ValentinMicheletV Offline
                  ValentinMicheletV Offline
                  ValentinMichelet
                  wrote on last edited by
                  #8

                  @raven-worx said:

                  just to add: you also need a C++11 supported compiler.

                  Well spotted.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mr. Kibu
                    wrote on last edited by
                    #9

                    Ok, a little bit better. But the "index.column()" gives me not the value of the column.
                    I have:

                    connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
                    

                    and

                    void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                    {
                        ui->change->setText("ccc");
                        ui->spinBox->setValue(index.column());
                    }
                    The QLAbel is "ccc" but the Spinbox is "0".
                    
                    Thank you!
                    raven-worxR 1 Reply Last reply
                    0
                    • M Mr. Kibu

                      Ok, a little bit better. But the "index.column()" gives me not the value of the column.
                      I have:

                      connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
                      

                      and

                      void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                      {
                          ui->change->setText("ccc");
                          ui->spinBox->setValue(index.column());
                      }
                      The QLAbel is "ccc" but the Spinbox is "0".
                      
                      Thank you!
                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @Mr.-Kibu said:

                      Ok, a little bit better. But the "index.column()" gives me not the value of the column.

                      instead of setting the value to the spinbox try the following and check the console:

                      void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                      {
                          qDebug() << Q_FUNC_INFO << "COLUMN:" << index.column();
                      }
                      

                      Since you set the column on the spinbox, you will only see what was the value of the last call of your slot. That's why i added a check for the column index in my previous post, which you completely left out in your last snippet.

                      --- 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
                      • M Offline
                        M Offline
                        Mr. Kibu
                        wrote on last edited by
                        #11

                        I get two messages:

                        void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 8
                        void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 0

                        I changed an value in column 8.

                        1 Reply Last reply
                        0
                        • raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

                          the dataChanged() signal is emitted everytime some data is changed which needs to be regathered with the model's data() method.
                          So this doesn't mean it has to be the display value only.

                          So simply check for the column index and only do your work for the desired column and ignore the calls for the rest of the columns and you're good to go.

                          --- 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
                          • M Offline
                            M Offline
                            Mr. Kibu
                            wrote on last edited by
                            #13

                            Ok, but how do I check for the index of the column I have edited?

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mr. Kibu
                              wrote on last edited by
                              #14

                              Hi, I have got a first success. If I edit column 8, I can calculate a value in column 7 like this:

                              the connect statement:

                              connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
                              

                              and then:

                              void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                              {
                                  qDebug() << Q_FUNC_INFO << "COLUMN:" << index.column();
                                  qDebug() << Q_FUNC_INFO << "ROW:" << index.row();
                              
                                  if(index.column() == 8){
                                     mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                  }
                              }
                              

                              I also get this debug-message on editing cell 1,8:

                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 8
                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 7
                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 0
                              void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                              

                              I don't know, why I get the debug-lines 5 and 6, and why the value in line 5 in 0 and in line 6 still 1 (??), but in works.

                              But now there is an new problem. I would like to edit both columns, column 8 and column 7. Editing column 8 will calculate column 7 and editing column 7 will calculate column 8. On adding a if-statement to the code to control editing column 7 the way I did it above with column 8, the program crashes after editing one of the columns (the dataCanged-Signal emits endless).

                              Is there a way to realise this, e.g. to emit the dataChanges-signal exactly on changes in a specific column?

                              Thank you!

                              raven-worxR 1 Reply Last reply
                              0
                              • M Mr. Kibu

                                Hi, I have got a first success. If I edit column 8, I can calculate a value in column 7 like this:

                                the connect statement:

                                connect(ui->tblCc->model(), &QSqlRelationalTableModel::dataChanged, this, &BVoucherPosForm::onCcDataChanged);
                                

                                and then:

                                void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                                {
                                    qDebug() << Q_FUNC_INFO << "COLUMN:" << index.column();
                                    qDebug() << Q_FUNC_INFO << "ROW:" << index.row();
                                
                                    if(index.column() == 8){
                                       mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                    }
                                }
                                

                                I also get this debug-message on editing cell 1,8:

                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 8
                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 7
                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 0
                                void BVoucherPosForm::onCcDataChanged(const QModelIndex&) ROW: 1
                                

                                I don't know, why I get the debug-lines 5 and 6, and why the value in line 5 in 0 and in line 6 still 1 (??), but in works.

                                But now there is an new problem. I would like to edit both columns, column 8 and column 7. Editing column 8 will calculate column 7 and editing column 7 will calculate column 8. On adding a if-statement to the code to control editing column 7 the way I did it above with column 8, the program crashes after editing one of the columns (the dataCanged-Signal emits endless).

                                Is there a way to realise this, e.g. to emit the dataChanges-signal exactly on changes in a specific column?

                                Thank you!

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

                                @Mr.-Kibu
                                add a simple recursion block:

                                void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                                {
                                    if( m_Block )
                                        return;
                                    m_Block = true;
                                
                                    // altering code ...
                                
                                   m_Block = false;
                                }
                                

                                --- 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
                                • M Offline
                                  M Offline
                                  Mr. Kibu
                                  wrote on last edited by
                                  #16

                                  Where do I have to write the calculation of the columns correct? I did it this way, but it fails:

                                  void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                                  {
                                  
                                      if(index.column() == 8){
                                         mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                      }
                                  
                                      bool m_Block;
                                      if( m_Block )
                                          return;
                                      m_Block = true;
                                  
                                      // altering code ...
                                      if(index.column() == 7){
                                          mModCc->setData(mModCc->index(index.row(),8),ui->dsbGTotal->value()/_Value*100,Qt::EditRole);
                                      }
                                  
                                     m_Block = false;
                                  }
                                  raven-worxR 1 Reply Last reply
                                  0
                                  • M Mr. Kibu

                                    Where do I have to write the calculation of the columns correct? I did it this way, but it fails:

                                    void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                                    {
                                    
                                        if(index.column() == 8){
                                           mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                        }
                                    
                                        bool m_Block;
                                        if( m_Block )
                                            return;
                                        m_Block = true;
                                    
                                        // altering code ...
                                        if(index.column() == 7){
                                            mModCc->setData(mModCc->index(index.row(),8),ui->dsbGTotal->value()/_Value*100,Qt::EditRole);
                                        }
                                    
                                       m_Block = false;
                                    }
                                    raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @Mr.-Kibu
                                    m_Block should of course be a member variable of your BVoucherPosForm class.
                                    And only where i wrote the comment "altering code" the actual code of the method should be placed.

                                    --- 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
                                    • M Offline
                                      M Offline
                                      Mr. Kibu
                                      wrote on last edited by
                                      #18

                                      Sorry, but there is no calculation on column 7 after editing column 8 :

                                          if( m_Block )
                                              return;
                                          m_Block = true;
                                      
                                          // altering code ...
                                          if(index.column() == 8){
                                             mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                          }
                                      
                                         m_Block = false;
                                      

                                      Where do I have to write the code to calculate column 8 after editing column 7?

                                      Thank you!

                                      raven-worxR 1 Reply Last reply
                                      0
                                      • M Mr. Kibu

                                        Sorry, but there is no calculation on column 7 after editing column 8 :

                                            if( m_Block )
                                                return;
                                            m_Block = true;
                                        
                                            // altering code ...
                                            if(index.column() == 8){
                                               mModCc->setData(mModCc->index(index.row(),7),ui->dsbGTotal->value()*_Percent/100,Qt::EditRole);
                                            }
                                        
                                           m_Block = false;
                                        

                                        Where do I have to write the code to calculate column 8 after editing column 7?

                                        Thank you!

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

                                        @Mr.-Kibu

                                        void BVoucherPosForm::onCcDataChanged(const QModelIndex& index)
                                        {
                                            if( m_Block )
                                                return;
                                        
                                            m_Block = true;
                                        
                                            switch( index.column() )
                                            {
                                                 case 7:
                                                 {
                                                        /* update column 8 */
                                                 }
                                                 break;
                                                 case 8:
                                                 {
                                                        /* update column 7 */
                                                 }
                                                 break;
                                           }
                                        
                                           m_Block = false;
                                        }
                                        

                                        --- 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