QComboBox of Model Items with same Textual Representation?



  • I have custom QComboBoxes to allow users to select one option from possible items in my data model. In order to perform relevant actions based on the selection, I reconstruct which item has been selected from my data model by checking the currentText. It is however possible that the items are represented by the same text, so this is not always working.

    I have a similar situation for custom QListWidgets, where I can however use QListWidgetItem to have a pointer to the appropriate item in my data model. Based on that I succeeded to resolve the same situation when items are represented by the same text in the QListWidget.

    What is the counterpart of QListWidgetItem for a QComboBox? QComboBoxItem does not exist... Or can somebody point me to an example of how I can solve my need for having a pointer to a custom class for the items in a QComboBox?


  • Moderators

    @ModelTech

    Possibly this thread is helping you



  • From what I understand from the delegates (which I was studying also in one of the Qt books), this allows editing the text shown by the QComboBox. My QComboBox is however not editable.

    In my case, I setup the QComboBox using QComboBox::addItem(const QString &text, const QVariant &userData = QVariant()). I don't use the last parameter but perhaps that is what I should do? Problem is that I don't understand how I can set the userData parameter to be a pointer to one of my own classes.

    To be a bit more concrete: I have subclassed QComboBox as follows.

    class SubScenarioSelector : public QComboBox {
        Q_OBJECT
    public:
        SubScenarioSelector(QWidget *, ProcessClass *, SubScenario *Selected);
        SubScenario *selection();
    private:
        ProcessClass *Specification;
    };
    

    with the following implementation:

    SubScenarioSelector::SubScenarioSelector(QWidget *Parent, ProcessClass *P, SubScenario *Selected) : QComboBox(Parent) {
        Specification = P;
        setInsertPolicy(QComboBox::InsertAlphabetically);
        for (int i = 0; i < Specification->numberOfSubScenarios(); ++i)
            addItem(QString(Specification->subScenario(i)->name()));
        setCurrentIndex(findText(Selected->name()));
    }
    
    SubScenario *SubScenarioSelector::selection() {
       SubScenario *Current = Specification->subScenario(0);
       for (int i = 0; i < Specification->numberOfSubScenarios(); ++i)
           if (Specification->subScenario(i)->name() == currentText()) {
               Current = Specification->subScenario(i);
               break;
           }
       return Current;
    }
    

    The problem is that the the name() of a subScenario() of the Specification can be the same for multiple items and in that case the line with setCurrentIndes(findText(Selected->name()) in the constructor and the SubScenarioSelector::selection() implementation do not work as desired.

    If the last parameter of QComboBox::addItem() in the constructor could be the SubScenario pointer returned by Specification->subScenario() then I see a way how to solve the rest of the challenge, but how does that work with QVariants?


  • Moderators

    @ModelTech

    When you have multiple times the same text in your combo box, you must have another criteria?



  • Yes, the differentiator would be the pointer to the relevant subscenario.



  • @ModelTech said in QComboBox of Model Items with same Textual Representation?:

    If the last parameter of QComboBox::addItem() in the constructor could be the SubScenario pointer returned by Specification->subScenario() then I see a way how to solve the rest of the challenge, but how does that work with QVariants?

    if SubScenario is a QObject then it just works without having to do anything. if it's not then store it as a void* and dynamic_cast it on retrieval (this assumes you don't delete the subscenario.

    P.S.
    I'm not a fan of your desing but let's go with it



  • SubScenario is indeed a QObject. Just tried using SubScenario* as second argument but that gives a compilation error as I expected.

    So, I would very much like to hear about your alternative design!



  • @ModelTech said in QComboBox of Model Items with same Textual Representation?:

    that gives a compilation error as I expected

    What error?

    Did you try using QVariant::fromValue()?



  • No, but that does solve my problem! Thanks.

    Still, I am curious to learn about your design alternative. If there is a better way to to things, I would really like to know.


Log in to reply
 

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