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. Sharing and storing selection of QComboBoxes

Sharing and storing selection of QComboBoxes

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 281 Views
  • 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.
  • I Offline
    I Offline
    Ignis
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • I Ignis

      @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?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #5

      @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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        Hi,

        You can share also the combobox selection model.

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

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #3

          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?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • I Offline
            I Offline
            Ignis
            wrote on last edited by Ignis
            #4

            @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?

            VRoninV 1 Reply Last reply
            0
            • I Ignis

              @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?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #5

              @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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2

              • Login

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