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 use SQLite-View as QCombobox-Model?
Forum Updated to NodeBB v4.3 + New Features

How to use SQLite-View as QCombobox-Model?

Scheduled Pinned Locked Moved General and Desktop
sqliteqcomboboxrelation
3 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mr. Kibu
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • fecubF Offline
      fecubF Offline
      fecub
      wrote on last edited by fecub
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr. Kibu
        wrote on last edited by
        #3

        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!!!

        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