QComboBox with QAbstractItemModel, default text problem

  • I have a model derived from QAbstractItemModel.

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


    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?


  • Lifetime Qt Champion


    What does your model contain ?

  • 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.

  • @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.

Log in to reply

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