【问题标题】:Is it possible to highlight only the text of the current QComboBox Selection?是否可以仅突出显示当前 QComboBox 选择的文本?
【发布时间】:2016-08-09 06:01:59
【问题描述】:

我只想突出显示当前 QComboBox 选择的文本而不是整个区域

直到向下箭头。为了更好地说明我想要这样的东西:

而不是这个:

这可能吗?如果是怎么办?我在网上搜索,尝试了几件事,但无法使其工作。有什么想法或建议吗?提前致谢。

【问题讨论】:

    标签: qt selection highlight qcombobox selectedtext


    【解决方案1】:

    您所描述的是 可编辑 组合框的默认行为。在这种情况下,只需设置

    QComboBox* box = new QComboBox();
    box->setEditable(true);
    

    如果您不希望您的QComboBox 可编辑,那么这是不直观的,但您想做的事情仍然可以完成。

    如果您将QComboBox 设置为可编辑,同时将基础行编辑设置为只读,则突出显示将与您的图片一样,但不会有任何光标并且用户将无法编辑组合框项目。这是一个例子:

    QComboBox* box = new QComboBox();
    box->addItems(QStringList() << "None (Min Profit)" << "All (Max Profit)");
    box->setEditable(true);
    box->lineEdit()->setReadOnly(true);
    
    // c++11 style, but this can also be done using SIGNAL(...) and SLOT(...)
    connect(box, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), [box]
    {
        box->lineEdit()->selectAll();
    });
    

    这是结果的图像(我在 Windows 10 上,所以样式有点滑稽)

    我的 2 美分:虽然可以做到,但对于不可编辑的项目,默认的 Qt 突出显示方案对用户来说可能更直观。

    【讨论】:

    • 从 Qt 5.7 和 C++14 开始,您可以使用qOverload,它比connect 中的static_cast 更具可读性:connect(box, qOverload&lt;int&gt;(&amp;QComboBox::activated), [box] { box-&gt;lineEdit()-&gt;selectAll(); });
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多