我对QComboBox
有奇怪的行为,基本上我监听currentIndexChanged信号,然后基于该索引从QStrList
填充第二个QComboBox
。我的问题是第二个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,请测试并确认。
您可以尝试执行此
ui->levelCombo->setCurrentIndex(-1); // deselect items, i have some proplems without this line
ui->levelCombo->setCurrentIndex(0); // select first item in combobox
我的Qt似乎有一个错误,我把成绩降到了Qt 4.8.3,问题就解决了。