您所描述的是
可编辑的
组合框。在这种情况下,只需设置
QComboBox* box = new QComboBox();
box->setEditable(true);
如果你不想
QComboBox
如果是可编辑的,那么它是不直观的,但是你想做的事情仍然可以完成。
如果将
Q组合框
为了可编辑,
同时将基础行编辑设置为只读
,则突出显示将与图片中的类似,但不会有任何光标,用户将无法编辑组合框项。下面是一个例子:
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突出显示方案可能对用户更直观。