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



  • 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.


  • Lifetime Qt Champion

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



  • [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.


  • Lifetime Qt Champion

    Maybe searching in the wrong widget ?



  • [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.


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

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