0

我有 8 个组合框,标签为 channel_1 ... channel_8。

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

我已经创建了这个插槽,但是创建的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
                {

                }
            }     
        }
    }
}

谁能帮我?

4

2 回答 2

1

您在堆栈上声明final_aand final_b,但随后将它们称为ui->final_aand ui->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
        }
    }
}
于 2013-02-04T16:17:02.133 回答
0

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

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

于 2013-02-04T16:18:34.730 回答