1

I want to show a text like "Please select one option" on the combobox, and don't show the text in the list, so I set setEditable to true, then set the text to the lineEdit, but after this, only the dropdown button (arrow) is clickable, how can we make the entire combobox clickable? I'm using the QComboBox as below:

QComboBox* combbox= new QComboBox;
combbox->setEditable(true);
combbox->lineEdit()->setReadOnly(true);
combbox->addItem("Option1");
combbox->addItem("Option2");
combbox->lineEdit()->setText("Please select one option");
ldlchina
  • 927
  • 2
  • 12
  • 31

4 Answers4

3

I resolved this problem as below:

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");
ldlchina
  • 927
  • 2
  • 12
  • 31
0

Making a QComboBox editable is problematic in terms of UI. I propose a different approach, reimplement a QComboBox and make a default item and remove it if the user clicks the combobox:

#include "mycombo.h"

MyCombo::MyCombo(QWidget *parent) :
    QComboBox(parent),
    defaultText_("Please select one option")
{
    addItem(defaultText_);
}

void MyCombo::mousePressEvent(QMouseEvent* event)
{
    if(this->currentText() == defaultText_)
    {
        this->removeItem(0);
    }

    QComboBox::mousePressEvent(event);
}

And then just create this combobox and insert the items where you want

MyCombo *combbox = new MyCombo(this);
combbox->addItem("Option1");
combbox->addItem("Option2");
Sommerwild
  • 506
  • 1
  • 6
  • 18
  • I considered this workaround. But It still has problem. The text "Please select one option" will disappear when mouse press on the combobox. I would like the text "Please select one option" is always there except user select one option. Are there any other qt controls can support this? Thanks. – ldlchina Jul 08 '15 at 08:25
  • So after the user has pressed an option the "Please select..." has to return? – Sommerwild Jul 08 '15 at 09:17
  • No, the "Please select one option" should disappear if user selected one option explicitly. I expect the "Please select one option" is still there when the dropdown list is displayed and user hasn't selected one option explicitly, don't select the first option as default. – ldlchina Jul 09 '15 at 03:04
0

You can use this lib : libqxt

You can find it here : https://bitbucket.org/libqxt/libqxt/wiki/Home

Use the object QxtCheckComboBox, with it you can check multiple items in your ComboBox.

0

The answer from IdlChina has one drawback: when you click the combobox when it's already shown, it hides and immediately shows again. I have a slightly different approach that doesn't have that problem.

class ButtonComboBox : public QComboBox
{
public:
    ButtonComboBox(QWidget *parent = nullptr)
        : QComboBox(parent),
          _isPopupShown(false),
          _timer(new QTimer(this))
    {
        auto lineEdit = new QLineEdit;
        lineEdit->installEventFilter(this);
        setLineEdit(lineEdit);

        _timer->setSingleShot(true);
        _timer->setInterval(100);
    }

protected:
    bool eventFilter(QObject *object, QEvent *event) override
    {
        if (object == lineEdit() && event->type() == QEvent::MouseButtonPress) {
            if (!_timer->isActive()) {
                if (!_isPopupShown)
                    showPopup();
                else if (_isPopupShown)
                    hidePopup();

                return true;
            }
        }

        return false;
    }

    void showPopup() override
    {
        QComboBox::showPopup();
        _isPopupShown = true;
        _timer->start();
    }

    void hidePopup() override
    {
        QComboBox::hidePopup();
        _isPopupShown = false;
        _timer->start();
    }

private:
    bool _isPopupShown;
    QTimer *_timer;
};
Jeruntu
  • 178
  • 1
  • 9