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 insert item to combobox that uses a QSQLquery model
QtWS25 Last Chance

How to insert item to combobox that uses a QSQLquery model

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.9k 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    Hello guys

    I have Combobox and SQLite. I read the database and load the data into the combobox. How can I add data to this combobox that does not exist in the database?

    @
    model->setQuery(query);
    ui->logindBList->setModel(model);

    I want to add this item to the list of data in the combo but it does not work
    ui->logindBList->addItem("localhost");
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      No, that does not work. It is either/or. Either you use an editable model like the default model, or you use an SQL query model. An SQL query model is not editable, so you cannot add items to it. You might try if it works with an QSqlTableModel instead, but I doubt it. Instead, you could read-in the data from the query, and add this data as items in your combobox. That will allow you to add new items, that you can then add back to the database.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        Thank you for your reply, but i dont know how to read the data from the modelquery
        do you have an example?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I'd not use the query model in in this case, but use a [[doc:QSqlQuery]] directly. The documentation of QSqlQuery contains an example on how to read the results.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

            I don't want to use QSQLQuery. Is there a way to copy the model into another editable model?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Sure, but that would be more complicated to do, I think. Did you try using a QTableModel instead?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SherifOmran
                wrote on last edited by
                #7

                I found this link which describes it. Thank you Andre

                http://apidocs.meego.com/1.2/qt4/sql-model.html

                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