In ComboBoxDelegate.cpp, I get the key variable:

 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *combo = static_cast<QComboBox*>(editor); model->setData(index, combo->currentText()); int comboRow = combo->currentIndex(); QModelIndex comboIndex = combo->model()->index(comboRow, 2); QVariant value = combo->model()->data(comboIndex); QString key = value.toString(); qDebug()<<key; } 

Next, I want to send it to pekarnya.cpp , where I will do the following with it:

 QSqlQuery query; query.prepare("UPDATE sklad SET sklad.weight = :weight WHERE sklad.key = :key;"); query.bindValue(":weight", result); query.bindValue(":key", key); query.exec(); 

How can I send a key and do it optimally?

Related issue .

  • And where does the result come from? Is it also like key going to transfer? - alexis031182
  • No, result is a variable inside the class Pekarnya - Leonty Kopytov

1 answer 1

You can create your own arbitrary signal in the delegate, which transmits the value of the key by which changes occurred in the original model. It is enough in the delegate class to declare an arbitrary signal and send it from the corresponding method:

 class ComboBoxDelegate : public QStyledItemDelegate { Q_OBJECT signals: void keyChanged(const QString &key); ... }; void ComboBoxDelegate::setModelData(QWidget *editor , QAbstractItemModel *model, const QModelIndex &index) const { // Так делать не надо! // QComboBox *combo = static_cast<QComboBox*>(editor); // Если речь идёт об указателе, связанном с QObject или QWidget, // используйте именно qobject_cast<T>(). QComboBox *combo = qobject_cast<QComboBox>(editor); if(combo == Q_NULLPTR) return; model->setData(index, combo->currentText()); int comboRow = combo->currentIndex(); QModelIndex comboIndex = combo->model()->index(comboRow, 2); QVariant value = combo->model()->data(comboIndex); emit keyChanged(value.toString()); } 

On the receiving side or, more simply, the slot that will be pre-connected to this signal, all that remains is to perform the appropriate processing:

 void Pekarnya::onSkladKeyChanged(const QString &key) { QSqlQuery query; query.prepare( "UPDATE sklad SET sklad.weight = :weight" \ " WHERE sklad.key = :key;"); query.bindValue(":weight", result); // "result" берётся извне. query.bindValue(":key", key); query.exec(); } 

From myself, I would still recommend to move away from this practice of synchronizing the relationships between tables at the code level in the application and, if possible, transfer responsibility for tracking changes directly to the database itself. These can be triggers or even procedures. Also, in some simple cases, it helps to simplify the task of using a bunch of ready-made QSqlRelation... classes QSqlRelation...

Comment

Since the ComboBoxDelegate::setModelData() const method is constant, respectively, in order to send a ComboBoxDelegate::keyChanged() signal from it, you will need to either declare the signal itself constant:

 signals: void keyChanged(const QString &key) const; 

Or cast the type of this pointer to a non-constant type:

 ComboBoxDelegate *delegate = const_cast<ComboBoxDelegate*>(this); emit delegate->keyChanged(value.toString());