我对QComboBox
有一些奇怪的行为,基本上我会监听currentIndexChanged信号,然后根据该索引从QComboBox
填充第二个QStrList
。我的问题是第二个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
上!在QComboBox
上插入超过10个项目会使其无法正确显示项目,如果我插入少于10个项目,则所有项目都会正确显示。
我的Qt版本是5.0.2,MinGW 4.7,请测试并确认。
答案 0 :(得分:0)
您可以尝试这样做
ui->levelCombo->setCurrentIndex(-1); // deselect items, i have some proplems without this line
ui->levelCombo->setCurrentIndex(0); // select first item in combobox
答案 1 :(得分:0)
好像我的Qt有一个bug,我的等级降到Qt 4.8.3并且问题消失了。