检查ComboBox中的文本是否与其他ComboBox相同

时间:2013-02-04 16:06:36

标签: qt qcombobox

我有8个ComboBox,标签为channel_1 ... channel_8。

我想检查用户是否在其中2个中选择了相同的选项,除了第一个选项'none'。

我已创建此广告位,但无法识别创建的final_afinal_b变量。

// Slot to check if there's two channels with the same option choosed
void gui::check_channels_options()
{
    for (int a = 1; a <= 8; a++)
    {
        for (int b = 1; b <= 8; b++)
        {
            if(a != b)
            {
                QString A, B;
                A.setNum(a);
                B.setNum(b);

                QString Na, Nb;
                Na = "channel_";
                Na += A;
                Nb = "channel_";
                Nb += B;

                QByteArray bytes_a = Na.toAscii();
                char* final_a = bytes_a.data();

                QByteArray bytes_b = Nb.toAscii();
                char* final_b = bytes_b.data();

                if((ui->final_a->currentText() == ui->final_b->currentText()) &&
                        (ui->final_a->currentIndex() != 0 && ui->fnal_b->currentIndex() != 0))
                {
                    QMessageBox::warning(this,"Error","Channel " + a + " has the same option as channel " + b,QMessageBox::Ok);
                }

                else
                {

                }
            }     
        }
    }
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您在堆栈上声明final_afinal_b,但随后将其称为ui->final_aui->final_b。尝试从中删除“ui->”。

总的来说,我认为您的方法可以简化。例如,假设您有指向存储在名为comboBoxes的数组中的组合框的指针。然后你可以这样做:

// create the combo boxes somewhere in your program, perhaps like this:
QComboBox *comboBoxes[8];
for (int i = 0; i < 8; ++i)
{
    comboBoxes[i] = new QComboBox;
}

// Slot to check if there's two channels with the same option choosed
void gui::check_channels_options()
{
    for (int a = 0; a < 8; ++a)
    {
        for (int b = 0; b < 8; ++b)
        {
            if (a == b ||
                comboBoxes[a]->currentText() == "none" ||
                comboBoxes[b]->currentText() == "none")
                continue; // no need to test these for equality
            else if (comboBoxes[a]->currentText() == comboBoxes[b]->currentText)
                // issue warning
            else
                // they are OK
        }
    }
}

答案 1 :(得分:0)

ui-&gt; final_a-&gt; currentText(),我不认为你可以通过这种方式访问​​UI元素。 UI中没有* final_a *元素,但我知道会有一些channel_1,channel2元素。

ps:请给变量一个有意义的名字,