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
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.
-
Hi,
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. -
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. -
I have the same problem and would be glad if someone could propose a solution.
-
@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.
-
@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.
-
@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 :(
-
"I would be glad to" is considered a polite form of speech.
-
@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.