Unsolved Data in a single column changes -->execute slot
-
@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; //... }
-
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?
-
remove the parameter name from the slot in the connect-statement.
-
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()" -
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.
-
@ValentinMichelet said:
If you use Qt5, you can use the new syntax:
just to add: you also need a C++11 supported compiler.
-
-
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!
-
@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.
-
I get two messages:
void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 8
void BVoucherPosForm::onCcDataChanged(const QModelIndex&) COLUMN: 0I changed an value in column 8.
-
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.
-
Ok, but how do I check for the index of the column I have edited?
-
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!
-
@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; }
-
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; }
-
@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. -
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!
-
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; }