Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sharing and storing selection of QComboBoxes



  • Hi, I'm pretty new to Qt GUI, so this may be a noob question.

    Here's what I'm trying to do: in my app there is a gigantic object that contains all choices available to the user for a long list of variables. When the user enters a certain mode in the app, he starts to make a choice of a certain number of values (often a single one) for every variable, mostly thorugh ```
    QComboBoxes or something similar. Other widgets in the same window are updated based on the choices made, so the user can play with different selections and see the results. The widgets here shown can be freely customized by the user.

    I already created a singleton that stores all available choices as cointainers, something like this:

    class AllChoicesSingleton
    {
    // Singleton technical stuff
    ...
    
    private:
    // Containers of available choices for every variable
    	QVector<A> AAvailableValues;
    	QList<B> BAvailableValues;
    	QVector<C> CAvailableValues;
    	...
    };
    

    After reading thorugh the model/view architecture documentation, I was going to change most of those container to proper class derived from QAbstractItemModel. That should make quite easy to fill the QComboBoxes and autoupdate them if the corresponding singleton's model/container changes.

    Is there a simple way to store the selections made by the user and share them among multiple widgets other than manually storing it and creating proper signals/slots to autoupdate them? Since, as I said, the widgets shown are customizable, the user can choose the same QComboBox twice and I need to coordinate those two so that if the user chooses one object for the first, the second one is automatically updated, showing the same item selected in the first.



  • @Ignis said in Sharing and storing selection of QComboBoxes:

    However the resulting comboboxes are independent as you can choose different values and nothing happens. Am I misunderstanding something?

    Yes, this

    currentIndex is not the same as the QItemSelectionModel in a combobox?

    They are 2 different things. the selection applies to the view inside the combobox dropdown, currentIndex is the item actually selected when you close the dropdown so you just need to set up a connection to handle currentIndexChanged


  • Lifetime Qt Champion

    Hi,

    You can share also the combobox selection model.



  • And if the widgets are using different source models (i.e. you are using different proxy models for each widget) you can use KLinkItemSelectionModel.

    You should make a bit more clear, however, what you mean by "selection" in the context of a combobox. Is it really selection or you actually mean currentIndex?



  • @VRonin Maybe I'm a bit confused myself, but currentIndex is not the same as the QItemSelectionModel in a combobox? Moreover, thanks for pointing out that you need a different class if there are proxy models, but for now, at least, I don't need it. I'll keep it in mind though.

    This is what I tried till now:

    To simplify it, let's suppose we have just one variable with two QComboBox attached to it. I'm experimenti with a class that has to deal with the widgets and holds the choices made:

    class ChoiceSheet: public QWidget
    {
    private:
    	QItemSelectionModel * selectedValue;
    	QComboBox * variableComboBox;
    	QComboBox * variableComboBox_2;
    	
    	// Other stuff
    };
    

    Here's the constructor:

    ChoiceSheet::ChoiceSheet(QWidget *parent) :
    	  QWidget(parent)
    {
    	// Initialization
    	selectedValue = new QItemSelectionModel(AllChoicesSingleton::getInstance().getModel(), this);
    	variableComboBox = new QComboBox(this);
    	variableComboBox_2 = new QComboBox(this);
    
    	// Specify the model for the comboboxes
    	variableComboBox->setModel(AllChoicesSingleton::getInstance().getModel());
    	variableComboBox_2->setModel(AllChoicesSingleton::getInstance().getModel());
    
    	// Forcing the same selection (what the class holds)
    	variableComboBox->view()->setSelectionModel(selectedValue);
    	variableComboBox_2->view()->setSelectionModel(selectedValue);
    }
    

    (I changed the singleton so that it holds a model for every variable; in this case it has just one model)

    However the resulting comboboxes are independent as you can choose different values and nothing happens. Am I misunderstanding something? Should I just use ignore selections and work on currentIndex with proper signals/slots connections?



  • @Ignis said in Sharing and storing selection of QComboBoxes:

    However the resulting comboboxes are independent as you can choose different values and nothing happens. Am I misunderstanding something?

    Yes, this

    currentIndex is not the same as the QItemSelectionModel in a combobox?

    They are 2 different things. the selection applies to the view inside the combobox dropdown, currentIndex is the item actually selected when you close the dropdown so you just need to set up a connection to handle currentIndexChanged


Log in to reply