【问题标题】:QComboBox Look Changes When I Use setEditable(true)使用 setEditable(true) 时 QComboBox 的外观发生变化
【发布时间】:2016-09-02 14:56:29
【问题描述】:

我想将 QComboBox 中的文本项居中对齐,但要做到这一点,我需要将 Editable 设置为 true。当我这样做时,外观会发生显着变化。这是在 Windows 7 上。

QComboBox when setEditable(false)

QComboBox when setEditable(true)

我认为发生这种变化是因为 QLineEdit 的默认外观与 QComboBox 不同。我该怎么做才能让它们看起来一样?谢谢,

【问题讨论】:

    标签: qt qcombobox qlineedit appearance


    【解决方案1】:

    当您启用 QComboBox 的可编辑状态时,默认小部件将更改为 QEditText,因此您必须使用 Qt Style Sheet 编辑 QEditText 样式:

    QLineEdit* lineEdit = new QLineEdit(parent);
    lineEdit->setStyleSheet("here customize");
    lineEdit->setAlignment(Qt::AlignCenter)
    ui->combo->setLineEdit(lineEdit);
    

    如果你不想编辑行编辑,试试这个:

    lineEdit->setReadOnly(true);
    ui->combo->setLineEdit(lineEdit);
    ui->combo->setEditable(true);
    

    另一种方法是,您可以自定义自己的 QComboBox 并修改 paintEvent(...)

    【讨论】:

    • 谢谢。我有点想通了,但我不知道如何找出不可编辑的 QComboBox 的默认样式设置,以便我可以将它们应用于可编辑的 QComboBox。我确实将只读设置为 true,但它仍然给了我不同的外观。
    猜你喜欢
    • 2012-01-18
    • 2017-09-26
    • 1970-01-01
    • 2021-05-15
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多