How to use SQLite-View as QCombobox-Model?



  • Hello!

    I am new in Qt and I try to make a little SQLite-Database Applikation.

    The Question is: Is ist possible (if yes, how) to use a SQLite-View as relationed table in a QCombobox?????

    I have tried to take a SQLite-Table to set the relation and it worked perfect. Then I changed the SQLite-Tabel with a SQLite-View and the data is not written to the database.

    What I did till now:

    First I connect to the database and set a new QSqiRelationalTableModel:

    modBelege = new QSqlRelationalTableModel(this, cn::db());
    modBelege->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
    modBelege->setTable("belege");
    

    Then I set the relation and set the model for the QCombobox (txtAdr):

    modBelege->setRelation(8, QSqlRelation("kontakte","AdrKey","Name"));//<----- "kontakte" is the SQLite-Table from the Database !!!!!!
    
    QSqlTableModel *relationModAdresse = modBelege->relationModel(8);
    relationModAdresse->setSort(1,Qt::AscendingOrder);
    relationModAdresse->select();
    ui->txtAdrKey->setModel(relationModAdresse);
    ui->txtAdrKey->setModelColumn(1);   // Index of the visible Column
    

    At least I make a new QDataWidgetMapper and map the data to the QCombobox:

    mapper=new QDataWidgetMapper(this);
    mapper->setModel(modBelege);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    
    mapper->addMapping(ui->txtAdrKey,8);
    
    mapper->toFirst();
    

    The code above works perfect, but "kontakte" is the SQLite-Table. If I change it to "vkontakte" (=SQLite-View) the data is not written to the database.

    Then I have tried the following:

    I let the relation to the SQLite-Table:

    modBelege->setRelation(8, QSqlRelation("kontakte","AdrKey","Name"));//<----- "kontakte" is the SQLite-Table from the Database !!!!!!
    

    And then I set the model for the QCombobox directly to the SQLite-View:

    QSqlTableModel *modAdresse=new QSqlTableModel();
    modAdresse=new QSqlTableModel();
    modAdresse->setTable("vKontakte");// <----- "vkontakte" is the SQLite-View from the Database !!!!!!
    modAdresse->select();
    

    Now the Index of the selected row in the QCombobox is written to the database and not the value of the foreign Key-Field (the first column of the view and the table)!!!!!!!

    It was not possible for me to write the value of the relationed Key-Field instead of the Index of the row to the database.

    Does anybody has a solution for my problem?????

    Thank you in advance!!

    Franz



  • if I understood your question correctly, for the solution with QSqlTableModel, the second one. You can do this:
    your_comboBox->setModelColumn(theNumberOfTheColumnInYourDatabaseWhichYouWillChangeAndShow);

    look here for detailed information: http://doc.qt.io/qt-5/qcombobox.html#modelColumn-prop



  • No, the column I am seeing in the combobox is the right column (the column with the text = column-ID 1).

    The value that is written into the database is not the value from the linked Key-Field ("AdrKey" = first column of "kontake" or "vkontake" = column-ID 0), but the Index (currentIndex) of the row that is selected in the combobox!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.