0

我有奇怪的行为QComboBox,基本上我听 currentIndexChanged 信号,然后根据该索引QComboBoxQStrList. 我的问题是第二个QComboBox被正常填充但没有正确显示文本,直到我点击项目然后它出现,我使用一个插槽来填充QComboBox.

InfoEmployer::InfoEmployer(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InfoEmployer)
{
    ui->setupUi(this);

    ui->gradeCombo->addItem("");
    ui->gradeCombo->addItem("200");
    ui->gradeCombo->addItem("300");
    ui->gradeCombo->addItem("400");
    ui->gradeCombo->addItem("500");
    ui->gradeCombo->addItem("600");
    ui->gradeCombo->addItem("700");

    QStringList levels;
    levels << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100" << "110" << "120";
    m_levelsContainer[0] = levels;
    levels.clear();
    levels << "11" << "22" << "33" << "44" << "55" << "66" << "77" << "88" << "99" << "110" << "120" << "131";
    m_levelsContainer[1] = levels;
    levels.clear();
    levels << "12" << "24" << "36" << "48" << "60" << "72" << "84" << "96" << "108" << "120" << "132" << "144";
    m_levelsContainer[2] = levels;
    levels.clear();
    levels << "13" << "26" << "39" << "53" << "66" << "79" << "92" << "105" << "118" << "132" << "145" << "158";
    m_levelsContainer[3] = levels;
    levels.clear();
    levels << "14" << "29" << "43" << "58" << "72" << "86" << "101" << "115" << "130" << "144" << "158" << "173";
    m_levelsContainer[4] = levels;
    levels.clear();
    levels << "16" << "32" << "47" << "63" << "79" << "95" << "110" << "126" << "142" << "158" << "173" << "189";
    m_levelsContainer[5] = levels;
    levels.clear();

    connect(ui->gradeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(getGradeLevels(int)));
}

// The slot
    void InfoEmployer::getGradeLevels(int indx)
    {
        if(indx != 0)
        {
            ui->levelCombo->clear();
            QStringList levels = m_levelsContainer[indx - 1];
            for(int i = 0; i < levels.length(); ++i)
                ui->levelCombo->addItem(levels[i]);
        }
    }

请注意,仅当我以QComboBox编程方式填充时才会发生此问题。

更新

我发现问题不在我的代码中,而是在QComboBox!插入超过 10 个项目QComboBox使其无法正确显示项目,如果我插入少于或 10 个项目,则所有项目都正确显示。我的 Qt 版本是 5.0.2 和 MinGW 4.7,请测试并确认。

4

2 回答 2

0

你可以尝试这样做

ui->levelCombo->setCurrentIndex(-1); // deselect items, i have some proplems without this line
ui->levelCombo->setCurrentIndex(0); // select first item in combobox
于 2013-08-02T05:52:37.587 回答
0

好像我的 Qt 有一个错误,我降级到 Qt 4.8.3 并且问题消失了。

于 2013-08-04T05:14:05.910 回答