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. [SOLVED] [QComboBox and Model/View] Associate PK and data in ComboBox
Qt 6.11 is out! See what's new in the release blog

[SOLVED] [QComboBox and Model/View] Associate PK and data in ComboBox

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 23.1k 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.
  • G Offline
    G Offline
    goetz
    wrote on last edited by
    #13

    [quote author="loladiro" date="1309266013"]
    You can always do what Volker suggested above (That accesses another column with the current index). If you think that's too much code everytime you want to access the index, I'm afraid you'll have to subclass QComboBox
    [/quote]

    That would just move that code into the subclass...

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #14

      Very true, I only suggested it, because he didn't seem to want to go through the entire process every time he access the index.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        valandil211
        wrote on last edited by
        #15

        Thanks, Volker. Now, that brings another question. I'm using something like that to fetch data from a DB.
        !https://lh6.googleusercontent.com/-Eyg_apku1S8/TgnRSDAAVnI/AAAAAAAAB34/YKLXdQehE84/s800/Screenshot-Contr%25C3%25B4les%2520de%2520qualit%25C3%25A9-1.jpg!

        The Category CC changes according to the Index of the GroupCC, as said earlier. Now, since I cannot set the Index of the ComboBox, the complexity of the query seems to increase rapidly (I need to account for the index of the group when binding a value to the category, etc...)

        By subclassing QComboBox, could I set the index myself, or is this really impossible? Thanks!

        Joey Dumont

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #16

          You get a signal when the group combo box changes. Get the data out of the group combo box' model (you have at least two choices pointed out here in the thread) and fill the model for the category's model accordingly. You'll have to re-select from the SQL anyways or put all the data in cache or the like which you access with the selected group's data.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valandil211
            wrote on last edited by
            #17

            Oh, the category CC already changes according to the index of the group CC (see the code I first posted).

            However, the five ComboBox uniquely identifies a set of data in the DB. Hence, I thought that if I could change the index of the ComboBox that I could directly use

            @query.prepare("...WHERE IndexAcc = :acc");
            query.bindValue(":acc", accComboBox->currentIndex();@

            because I thought that using something like

            @
            query.prepare("...WHERE name_acc = :acc");
            query.bindValue(":acc", accComboBox->currentText().toString());@

            was error-prone. Ain't it?

            Joey Dumont

            1 Reply Last reply
            0
            • V Offline
              V Offline
              valandil211
              wrote on last edited by
              #18

              [quote author="loladiro" date="1309266013"]
              You can always do what Volker suggested above (That accesses another column with the current index). If you think that's too much code everytime you want to access the index, I'm afraid you'll have to subclass QComboBox
              [/quote]

              Okay, now I understand this part better. However, what I need is relational table, because for one value of the GroupComboBox index, I have several possible values for the Category ComboBox index.

              Joey Dumont

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #19

                A primary key is a primary key - a string value need not be unique.

                So you're doing it the wrong way anyways.

                query.bindValue() sets the value to the current value of that variable. It is not updated once the value changes! No matter how often you run the query, it always yields the same results!

                You must call bindValue again after the value has changed (i.e. after the combo box has a new current item) and re-run the query!

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  valandil211
                  wrote on last edited by
                  #20

                  [quote author="Volker" date="1309267439"]A primary key is a primary key - a string value need not be unique.

                  So you're doing it the wrong way anyways.

                  query.bindValue() sets the value to the current value of that variable. It is not updated once the value changes! No matter how often you run the query, it always yields the same results!

                  You must call bindValue again after the value has changed (i.e. after the combo box has a new current item) and re-run the query![/quote]

                  Yes, I know all that. This sort of query will be connected to a button (now yet implemented) that will bind the values of whatever is in the ComboBoxes to a prepared SQL query and update the UI accordingly. However, from what you're telling me, I can access the PK associated with a certain item in the groupComboBox by using the currentIndex() of the groupComboBox and externally mapping it to its PK. That's fine.

                  However, for each item in the groupComboBox, there are several items in the categoryComboBox. Now, since each time I change the index of groupComboBox, the contents of the categoryComboBox are changed, I cannot map the PK of the categoryComboBox to its index. Do you understand my problem?

                  Because of this one-to-many relationship, it is somewhat difficult to implement something that would bind the right value in the SQL query. Now, and I want this to be the final question in this grudgingly long thread, can I set the indices of ComboBoxes if I subclass them?

                  (To me, it seems like the most robust solution to fetch data from the DB.)

                  Thanks, y'all!

                  Joey Dumont

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #21

                    Sorry, I don't understand where your mapping breaks. And I don't see a potential problem either.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      valandil211
                      wrote on last edited by
                      #22

                      Okay. I'll work a little bit more on this and come back later.

                      Thanks for your tips!

                      Joey Dumont

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        valandil211
                        wrote on last edited by
                        #23

                        It was easier that I thought. I wanted to filter data according to a foreign key. I had to fetch the PK of a table according to the value of the ComboBox.

                        Here's how I did it:

                        @
                        catModel->setFilter(""CAT_IndexGroupe" = " % QString::number(
                        groupModel->data(groupModel->index(ui->MW_groupComboBox->currentIndex(), groupModel->fieldIndex("IndexGroupe")), Qt::DisplayRole).toInt()));@

                        Thanks everyone for your tips!

                        Joey Dumont

                        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