Ingredients

Stock

There are two tables: Warehouse and Ingredients, interconnected by a foreign key by id . Arranged ComboBoxDelegate in QTableView . ComboBox takes data from the Warehouse table in the Weight column. But there is such a moment that it displays the entire column completely, and I need each row to have a choice of "weight" for the corresponding ID. At the moment, the status of the code is as follows:

 ComboBoxDelegate::ComboBoxDelegate(QObject *parent) { DelegateModel = new QSqlQueryModel; DelegateModel->setQuery("SELECT weight FROM sklad, ingridient WHERE sklad.idsklad=ingridient.iding;"); } QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QComboBox *editor = new QComboBox(parent); editor->setModel(DelegateModel); return editor; } 

Tell me who can do it, but already Moscow has broken how to do it.

    2 answers 2

     QWidget *ComboBoxDelegate::createEditor(QWidget *parent , const QStyleOptionViewItem &option , const QModelIndex &index) const { // ИндСкс ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ с ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π°ΠΌΠΈ ΠΈΠ½Π³Ρ€Π΅Π΄ΠΈΠ΅Π½Ρ‚ΠΎΠ². const int iding_col = 0; const QVariant iding = index.model()->data(index.model() ->index(index.row(),iding_col)); if(iding.isValid() == false) return Q_NULLPTR; QSqlQuery query; query.prepare( "SELECT `weight` FROM `sklad`" \ " WHERE `idsklad` = :idsklad"); query.bindValue(":idsklad", iding); if(query.exec() == false) return Q_NULLPTR; QComboBox *editor = new QComboBox(parent); // QStyleOptionViewItem содСрТит мноТСство ΠΏΠΎΠ»Π΅Π·Π½Ρ‹Ρ… Π²Π΅Ρ‰Π΅ΠΉ // для построСния ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎ выглядящСго Π²ΠΈΠ΄ΠΆΠ΅Ρ‚Π° Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€Π°. editor->setGeometry(option.rect); // ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ€Π°Π· ΠΏΡ€ΠΈ создании Π²ΠΈΠ΄ΠΆΠ΅Ρ‚Π° Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€Π° // Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒΡΡ ΠΈ Π½ΠΎΠ²Ρ‹ΠΉ инстанс ΠΌΠΎΠ΄Π΅Π»ΠΈ, // ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ Π²Ρ‹Π±ΠΎΡ€ΠΊΠ° Π΄Π°Π½Π½Ρ‹Ρ… Ρ€Π°Π·Π»ΠΈΡ‡Π½Π° для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· строк. // Установка ΠΆΠ΅ Π²ΠΈΠ΄ΠΆΠ΅Ρ‚Π° Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€Π° Π² качСствС родитСля ΠΌΠΎΠ΄Π΅Π»ΠΈ // позволяСт Π½Π΅ Π·Π°Π±ΠΎΡ‚ΠΈΡ‚ΡŒΡΡ ΠΎ своСврСмСнном ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠΈ послСднСй. QSqlQueryModel *model = new QSqlQueryModel(editor); model->setQuery(query); editor->setModel(model); return editor; } 
    • Thank you, that is necessary! - Leonty Kopytov
    • There is another question. Let's say that I will get a unique key in the Warehouse table. Is it possible to somehow get the key of the item that I chose in the ComboBox, so that later it was possible to edit the records through the ComboBox? - Leonty Kopytov
    • Yes you can. First, get the index of the selected element QComboBox , then from the model connected to it, by the column name, you get the value of the primary key of the record. You can also use the QSqlRelation... classes QSqlRelation... , but they are not suitable for all cases. - alexis031182
    • I understand this is done in setModelData? I apologize for the stupid questions, with the delegates are not yet familiar) - Leonty Kopytov
    • one
      Yes, it is in this delegate method. - alexis031182

    Tell me who can do it, but the Mosk has already broken how to do it

    It is necessary to edit the query:

     SELECT weight FROM sklad, ingridient WHERE sklad.idsklad=ingridient.iding AND ingridient.iding = число 

    A "number" is calculated by QModelIndex . I would dig this way.

    • Thank you, I will try - Leonty Kopytov
    • However ... I could be wrong, but you have a little wrong with business logic. "Available" - should not be editable, but should be computable, as the difference between the SUM () of the product in stock, and the SUM () used. Well this is IMHO, you know better there. - Majestio
    • The fact of the matter is that initially I had just cells with a sum, but when I clicked on the button, this sum should decrease, which means I need to write down the rest somewhere and then I had problems, so I decided to do ComboBox. In principle, this is only a manufacturing practice and most likely will not be used anywhere. - Leonty Kopytov