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. QComboBox with QAbstractItemModel, default text problem
Qt 6.11 is out! See what's new in the release blog

QComboBox with QAbstractItemModel, default text problem

Scheduled Pinned Locked Moved General and Desktop
qcomboboxqabstractitemmo
10 Posts 5 Posters 6.2k Views 4 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.
  • A Offline
    A Offline
    applefier
    wrote on last edited by
    #1

    I have a model derived from QAbstractItemModel.

    In several places I use non-editable QComboBox widgets to display different parts of data. After setting

       comboBox->setModel(model);
       comboBox->setRootModelIndex(index);
    

    all combo boxes contain proper data, so functionally they are great. However, the text displayed by these QComboBox before any selection has been done is always the same, regardless of the QModelIndex given to them, which is not what I want.
    For this purpose, all combo boxes are showing a QString which is located in the first child of the model, for reason unknown to me.

    Any idea why my combo boxes are looking for that text where they are? Much more importantly, how do I set that text to what I want it to be, which is different for each combo box?

    Thanks.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What does your model contain ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • A Offline
        A Offline
        applefier
        wrote on last edited by
        #3

        Hi SGaist,

        The model has a tree structure and contains tables. Depending on the table, they show doubles, strings, boolean etc. For all but three tables, QTableView is used and for those remaining three it's QComboBox. (actually, classes derived from those two)

        All tables have a private member of std::string type (not QString as I wrote in the first post, though it makes no difference), whose value is the table's title and which is set using a non-qt method.

        I wasn't expecting QComboBox to automatically find that string and display it as default text (i.e. when no selection has been done) but I was expecting to be able to instruct the QComboBox where to find that string. Apparently, things don't work like that.
        However, what bugs me is that all three QComboBox widgets were able to automatically find one of these strings - the one in the first table of the model - and this is what they all show as default text. If they are capable of finding that one, how come they don't look for the same thing inside their own index, because they would find it? Again, all these std::string's are set using the same method, so there is no difference between them, except in their location.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What does your model contain ?

          A Offline
          A Offline
          applefier
          wrote on last edited by
          #4

          @SGaist

          I have found out that I can use setEditText(const QString &) to specify the default display text, like "-- Choose --". But, this works only with editable QComboBox widgets, which I wouldn't want to do. Besides, why is my non-editable QComboBox displaying a seemingly arbitrary string that has nothing to do with the provided index? Again, it is only the display text I am talking about, while the selectable items - the contents of the ComboBox - are perfectly fine.
          How does this work to begin with? I have read the documentation on QComboBox numerous times and found nothing.

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Michael Scopchanov
            wrote on last edited by
            #5

            I have the same problem and would be glad if someone could propose a solution.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Michael-Scopchanov said in QComboBox with QAbstractItemModel, default text problem:

              I have the same problem and would be glad if someone could propose a solution.

              Please provide a minimal, compilable example to show your problem - actually the problem your hijacked here is more than 4 years old and I never had such a problem so maybe it's already fixed.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Michael-Scopchanov said in QComboBox with QAbstractItemModel, default text problem:

                I have the same problem and would be glad if someone could propose a solution.

                Please provide a minimal, compilable example to show your problem - actually the problem your hijacked here is more than 4 years old and I never had such a problem so maybe it's already fixed.

                M Offline
                M Offline
                Michael Scopchanov
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in QComboBox with QAbstractItemModel, default text problem:

                the problem your hijacked here

                My post already contains a minimal example. Of how to be polite.

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Michael-Scopchanov said in QComboBox with QAbstractItemModel, default text problem:

                  My post already contains a minimal example

                  Where? I don't see one - did you try to upload an attachment? This sadly does not work :(

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Michael Scopchanov
                    wrote on last edited by
                    #9

                    "I would be glad to" is considered a polite form of speech.

                    kshegunovK 1 Reply Last reply
                    0
                    • M Michael Scopchanov

                      "I would be glad to" is considered a polite form of speech.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @Michael-Scopchanov said in QComboBox with QAbstractItemModel, default text problem:

                      "I would be glad to" is considered a polite form of speech.

                      Provide some minimal example or code if you expect someone to look at it, comment and try to help you. This kind of arguing does not, nor is going to, get you anywhere.

                      PS. Notice I didn't use "please", "would you", "be so kind as to" etc., because these words have no meaning in a technical discussion. We want facts and well structured problems to take a stab at and have some fun, and you want answers; it's a symbiosis and we don't get paid for our time. So unless someone's been openly hostile or have shown malice, which nobody had, then just as a friendly advice you'd do yourself good to grow some thicker skin.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved