c++ - QComboBox : Can we make the entire combobox clickable, 不只是下拉按钮(箭头)本身?

标签 c++ qt combobox

我想在组合框中显示“请选择一个选项”之类的文本,并且不在列表中显示文本,所以我将 setEditable 设置为 true,然后将文本设置为 lineEdit,但在此之后,只有下拉按钮(箭头)是可点击的,我们怎样才能让整个组合框都可以点击呢?我正在使用 QComboBox,如下所示:

QComboBox* combbox= new QComboBox;
combbox->setEditable(true);
combbox->lineEdit()->setReadOnly(true);
combbox->addItem("Option1");
combbox->addItem("Option2");
combbox->lineEdit()->setText("Please select one option");

最佳答案

我解决了这个问题如下:

class QTComboBoxButton : public QLineEdit
{
    Q_OBJECT
public:
    QTComboBoxButton(QWidget *parent = 0);
    ~QTComboBoxButton();

protected:
    void mousePressEvent(QMouseEvent *);
};

QTComboBoxButton::QTComboBoxButton(QWidget *parent /* = 0 */) :
    QLineEdit(parent)
{
}

QTComboBoxButton::~QTComboBoxButton()
{
}

void QTComboBoxButton::mousePressEvent(QMouseEvent * e)
{
    QComboBox* combo = dynamic_cast<QComboBox*>(parent());
    if(combo)
        combo->showPopup();
}

QComboBox* combbox= new QComboBox;
combbox->setEditable(true);
combbox->setLineEdit(new QTComboBoxButton(combbox));
combbox->lineEdit()->setReadOnly(true);
combbox->addItem("Option1");
combbox->addItem("Option2");
combbox->lineEdit()->setText("Please select one option");

关于c++ - QComboBox : Can we make the entire combobox clickable, 不只是下拉按钮(箭头)本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197772/

相关文章:

c++ - 我的 header 中需要 #include 指令吗?

c++ - QByteArray::append() 导致内存重新分配

mvvm - 如何将 Combobox 的选定项绑定(bind)到两个不同的属性?

c++ - 按下 X 时 QDialog 没有立即关闭,如何让它不在顶部?

c++ - 我应该如何在 vector 中获取一系列输入?

c++ - CUDA错误: expected constructor,析构函数,或者 'void'之前的类型转换

c++ - 在 Qt 中将 QString 转换为 Ascii 值,反之亦然

c++ - Qt tableView - 添加颜色

javascript - 如何填充asp :Textbox using value in ComboBox

c# - dataGridView ComboBox 事件处理程序问题