Skip to content
  • 0 Votes
    5 Posts
    314 Views
    SGaistS

    @JonB one of the issue is that the database initialization is not done in the correct order and assumes the values of the cities ID.

    All in all, it should use a Foreign Key to properly map the city to the user. This would properly structure the relation between the tables.

  • 0 Votes
    2 Posts
    469 Views
    D

    Hi again !
    It seems that by looking still a bit more I found a suitable answer.
    The key was to set the QSqlRelationalDelegate for the mapper and not the QComboBox but still setting the model of the QComboBox to the table containing all the functions.

    Here is my final code:

    //Init all private members //Init all private members price1 = new QDoubleSpinBox(this); price2 = new QDoubleSpinBox(this); price3 = new QDoubleSpinBox(this); price4 = new QDoubleSpinBox(this); function = new QComboBox(this); mapper = new QDataWidgetMapper(this); sqlModel = new QSqlRelationalTableModel(this); QSqlTableModel *fctModel = new QSqlTableModel(this); QListView *articlesView = new QListView(this); //Imediately links the views and the models sqlModel->setTable("Articles"); //A few indexes int nameIndex = sqlModel->record().indexOf("Name"); int priceIndex = sqlModel->record().indexOf("sellPrice"); int jobShareIndex = sqlModel->record().indexOf("jShare"); int bPriceIndex = sqlModel->record().indexOf("bPrice"); int redPriceIndex = sqlModel->record().indexOf("reducedPrice"); int functionIndex = sqlModel->record().indexOf("function"); int functionNameIndex; { auto tmp = new QSqlTableModel(); tmp->setTable("Functions"); functionNameIndex = tmp->record().indexOf("name"); delete tmp; } //Configures the sql model sqlModel->setRelation(functionIndex, QSqlRelation("Functions", "Id", "name")); sqlModel->setEditStrategy(QSqlTableModel::OnFieldChange); sqlModel->select(); //Views articlesView->setModel(sqlModel); articlesView->setModelColumn(nameIndex); //Sets the column to the name articlesView->setEditTriggers(QAbstractItemView::NoEditTriggers); function->setModel(sqlModel->relationModel(functionIndex)); function->setModelColumn(functionNameIndex ); //Mapper mapper->setModel(sqlModel); mapper->addMapping(price1, priceIndex); mapper->addMapping(price2, bPriceIndex); mapper->addMapping(price3, jobShareIndex); mapper->addMapping(price4, redPriceIndex); mapper->addMapping(function, functionIndex); mapper->setItemDelegate(new QSqlRelationalDelegate(mapper)); mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);

    I hope that it will be able to help anybody who might encounter that same problem !
    Happy new year everybody, take care ! :)

  • 0 Votes
    6 Posts
    1k Views
    K

    So my final solution is:

    class SqlRelationalDelegate : public QSqlRelationalDelegate { Q_OBJECT public: StorageSqlRelationalDelegate(QObject* parent = nullptr) : QSqlRelationalDelegate(parent) {} QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { editor = QSqlRelationalDelegate::createEditor(parent, option, index); return editor; } void destroyEditor(QWidget* editor, const QModelIndex& index) const override { QSqlRelationalDelegate::destroyEditor(editor, index); this->editor = nullptr; } QWidget* getEditor() const { return editor; } private: mutable QWidget* editor = nullptr; }; ... class TableView : public QTableView { Q_OBJECT public: StorageTableView(QWidget* parent = nullptr) {} void openPersistentEditor(const QModelIndex &index) { QTableView::openPersistentEditor(index); emit persistentEditorOpened(index); } signals: void persistentEditorOpened(const QModelIndex &index); }; ... connect(view, &TableView::persistentEditorOpened, [view] (const QModelIndex& index) { auto model = view->model(); auto delegate = qobject_cast<SqlRelationalDelegate*>(view->itemDelegate(index)); if (!delegate) qFatal("Delegate cast error"); delegate->setModelData(delegate->getEditor(), model, index); });
  • 0 Votes
    9 Posts
    2k Views
    JonBJ

    @VRonin
    Ah. I believe the user described the interface he gets currently (with his QTableView). Whether he likes it or not is a different matter.

    Then his question was: how does he get a notification when, having created a new row, it is actually sent to the database for INSERT, given that there is no signal for this in his case ("editStrategy is OnFieldChange").

    My overriding QSqlTableModel::insertRowIntoTable() is how to achieve that. So yours is to do with the interface for adding a new row, right? Whether he would benefit/prefer to change over to that to I cannot say :)

  • 0 Votes
    2 Posts
    795 Views
    mrjjM

    Hi
    Did you have a look at
    http://doc.qt.io/qt-4.8/qitemeditorfactory.html#details

    Seems to be such editor cache, like you have/want.