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. How to setData in foreign key table of QSqlRelationalTableModel via QSortFilerProxyModel?

How to setData in foreign key table of QSqlRelationalTableModel via QSortFilerProxyModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 152 Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    The following situation exists:
    An SQLite database with an N:M relationship. The whole thing is solved via a table between

    Table: Deutscher_Pilzname

    +----------------------------------+
    | Id | Pilz_id | Deutscher_name_id |
    +----------------------------------+
    | 1  |   700   |         1         |
    | 2  |    33   |         2         |
    | .. |   ...   |        ...        |
    +----------------------------------+
    

    Table: Deutscher_Name

    +----+----------------+
    | Id | Deutscher_Name |
    +----+----------------+
    |  1 | Name_1         |
    |  2 | Name_2         |
    | .. | ...            |
    +----+----------------+
    

    Now I'm getting the data out via a QSqlRelationTableModel.

    QSqlRelationalTableModel *DatabaseManager::germanFungusNameDataModel() const
    {
        QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
        model->setTable("Deutscher_Pilzname");
        model->setRelation(model->fieldIndex("Deutscher_name_id"), QSqlRelation("Deutscher_Name", "Id", "Name"));
        model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
        model->setSort(2, Qt::AscendingOrder);
    //    model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
        model->select();
        while (model->canFetchMore()) {
            model->fetchMore();
        }
        return model;
    }
    

    In the constructor of my mainwindow.cpp I create a subclassed QSortFilerProxyModel and set the source Model and set it to a QListView.

    //QSqlRelationTableModel
    mGermanFungusNameDataModel = mDatabaseManager->germanFungusNameDataModel();
    
    //Subclassed QSortFilerProxyModel
    mGermanNameDataFilter->setSourceModel(mGermanFungusNameDataModel);
    mGermanNameDataFilter->sort(Column::GermanName);
    mGermanNameDataFilter->setSortCaseSensitivity(Qt::CaseInsensitive);
    
    //QListView
    mGermanNameEditor->setModel(mGermanNameDataFilter);
    mGermanNameEditor->setModelColumn(Column::GermanName);
    

    Now I have created a QDialog where I cann change the text and after that I call this:

        const bool germanNameIsSelected = mGermanNameEditor->selectionModel()->hasSelection();
                    QModelIndex index = mGermanNameEditor->currentIndex();
                    QModelIndex sourceIndex = mGermanNameDataFilter->mapToSource(index);
                    GermanNameEditor editor(this);
                    if(germanNameIsSelected){
                        QString name = index.data().toString();
                        editor.setCurrentText(name);
                        editor.setModal(true);
                        const int type = editor.exec();
                
                        if(type == QDialog::Accepted){
                            QString changedName = editor.currentText();
                            auto value = sourceIndex.siblingAtColumn(2).data().toString();
                
                            while(mGermanFungusNameDataModel->relationModel(2)->canFetchMore()){
                                mGermanFungusNameDataModel->relationModel(2)->fetchMore();
                            }
                            qDebug() << "RowCount relation: " << mGermanFungusNameDataModel->relationModel(2)->rowCount();
                            for(int i=0 ;i<mGermanFungusNameDataModel->relationModel(2)->rowCount();i++){
                                const QModelIndex index = mGermanFungusNameDataModel->relationModel(2)->index(i, 1);
                                const QVariant data = index.siblingAtColumn(1).data();
                                if(data.toString() == value) {
                                   qDebug() << "setData relationModel: " << mGermanFungusNameDataModel->relationModel(2)->setData(index, changedName, Qt::EditRole);
                                   mGermanFungusNameDataModel->database().transaction();
                                   qDebug() << "Submitall: " << mGermanFungusNameDataModel->submitAll();
                                   mGermanFungusNameDataModel->database().commit();
                                   break;
                                }
                            }
    

    setData returns true and submitAll as well, but the whole thing is not changed. Neither in the GUI nor in the database. This is probably related to my subclassed QSortFilterProxyModel. I thought when the sourceModel is changed the QSortFilerProxyModel is updated automatically.

    Does anyone have any idea?

    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