问题点
QComboBox下拉框无法通过QSS设置下拉框的item高度;
知识点
利用 QComboBox的model(QStandardItemModel)对下拉框的每个项进行高度设置;
编码
// testWidget.cpp
{
QComboBox * comboBox = new QComboBox(this);
comboBox->setGeometry(300, 300, 200, 32);
comboBox->addItems({ "height: 0px", "height: 15px", "height: 30px", "height: 45px", "height: 60px" });
// 设置高度
int height = 0;
QStandardItemModel * model = qobject_cast<QStandardItemModel*>(comboBox->model());
for (int i = 0; i < model->rowCount(); ++i)
{
QStandardItem * item = model->item(i);
item->setSizeHint({ 0, height++ * 15 });
}
}