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. [SOLVED] How to only allow one instance of a value across multiple QComboBoxes?

[SOLVED] How to only allow one instance of a value across multiple QComboBoxes?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.3k 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
    iamthesgt
    wrote on last edited by
    #1

    I am building an application that can be used for create tournament pools. I can create pools and add QComboBoxes to the Pool objects that are populated from a list of teams. However, I want to, when one ComboBox has a Team selected, if any other ComboBoxes have that team already selected, to change the other ComboBox to the default value ("Select").

    I got what I think will work, but unfortunately when it goes through all ComboBoxes of that type, it of course finds the box that was just selected and sets it to the default value.

    Code:
    @void Pool::createPool()
    {
    QLabel *seed = new QLabel("1");
    QComboBox *team = new QComboBox();
    QPushButton *addTeamtoPool = new QPushButton("+");

    QHBoxLayout *poolTeam = new QHBoxLayout();
    poolTeam->addWidget(seed);
    poolTeam->addWidget(team);
    
    container->addLayout(poolTeam);
    container->addWidget(addTeamtoPool);
    
    team->insertItems(0, _teams);
    team->insertItem(0, "Select");
    team->setCurrentText("Select");
    
    connect(addTeamtoPool, SIGNAL(clicked()), this, SLOT(addPoolSlot()));
    connect(team, SIGNAL(currentTextChanged(QString)), this, SLOT(updateSelectedDDL(QString)));
    

    }

    void Pool::updateSelectedDDL(QString changedName)
    {
    if(changedName != "Select")
    {
    QList<QWidget*> list = this->parent()->findChildren<QWidget*>();
    QList<QWidget*>::iterator k;
    for(k = list.begin(); k != list.end(); k++)
    {
    QComboBox cmBox = qobject_cast<QComboBox>(*k);
    if(cmBox)
    {
    if(cmBox->currentText() == changedName)
    {
    cmBox->setCurrentText("Select");
    }
    }
    }
    }
    }@

    I need some way to identify the original box that was changed and then exclude that one from being changed or some other way to modify all other ComboBox objects.

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

      Hi and welcome to devnet,

      You can use the sender() function to get the combo box from which the signal call originated.

      Also, why don't use use directly findChildren<QComboBox*> ?

      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
      0
      • I Offline
        I Offline
        iamthesgt
        wrote on last edited by
        #3

        [quote author="SGaist" date="1411934984"]
        You can use the sender() function to get the combo box from which the signal call originated.[/quote]

        Thanks, that's exactly what I needed. Works perfectly!

        [quote author="SGaist" date="1411934984"]
        Also, why don't use use directly findChildren<QComboBox*> ?[/quote]

        I was initially doing that, but I was getting an empty list for some reason and I could never figure it out. The problem "magically" went away when I changed to the above method. I still have no idea what the problem was.

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

          Maybe searching in the wrong widget ?

          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
          0
          • I Offline
            I Offline
            iamthesgt
            wrote on last edited by
            #5

            [quote author="SGaist" date="1411939831"]Maybe searching in the wrong widget ?[/quote]

            Maybe. I just tried doing that in a different method and it worked so I'm not sure what I was doing wrong last time.

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

              Well, doesn't really matter now that you succeeded :)

              Since you have all working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

              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
              0

              • Login

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