QT 中QComBoBox setView()的注意事项

114 阅读1分钟
/**
 * @brief 设置QCombobox间距
 */
void purealgorithm_2::setComboboxSpace(QComboBox *com)
{
    QString str = "QComboBox QAbstractItemView:item\
    \n{\
        font-family: PingFangSC-Regular;\n\
        font-size:9px;\n\
        min-height:20px;\n\
        min-width:20px;\n\
    }\n";
    QListView* listView = new QListView();
    com->setView(listView);
    com->setStyleSheet(str);
}


    //QComBoBox单位间隔设置
    pA2.setComboboxSpace(ui->comboBox_18);
    pA2.setComboboxSpace(ui->comboBox_26);
    pA2.setComboboxSpace(ui->comboBox_34);
    pA2.setComboboxSpace(ui->comboBox_47);
    pA2.setComboboxSpace(ui->comboBox_48);
    pA2.setComboboxSpace(ui->comboBox_49);
    pA2.setComboboxSpace(ui->comboBox_50);
    pA2.setComboboxSpace(ui->comboBox_51);
    pA2.setComboboxSpace(ui->comboBox_52);
    pA2.setComboboxSpace(ui->comboBox_53);
    pA2.setComboboxSpace(ui->comboBox_54);

在使用QListView对象来调整ComBoBox的内容间距等时,需要注意一个QListView对象只能作用于一个QComBoBox,如果想要调整多个控件,建议如上封装函数,多次调用,这样就很方便初始化多个QListView对象来调整内容间距等。

注意:多次调用的后果是只有最后一个QComBoBox生效,前面的所有QComBoBox将会出现无法打开的情况,也就是下拉框无法打开!!!