【问题标题】:QComboBox: Can we make the entire combobox clickable, not just the dropdown button (arrow) itself?QComboBox:我们能否让整个组合框都可点击,而不仅仅是下拉按钮(箭头)本身?
【发布时间】:2015-07-03 03:03:05
【问题描述】:

我想在组合框中显示“请选择一个选项”之类的文本,并且不在列表中显示文本,所以我将 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");

【问题讨论】:

  • 您可以处理点击事件并展开它。
  • 不要让它可编辑;当用户选择某物时,请从组合框内容中删除“请选择一个选项”。

标签: c++ qt combobox


【解决方案1】:

我解决了这个问题如下:

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");

【讨论】:

    【解决方案2】:

    使 QComboBox 可编辑在 UI 方面是有问题的。 我提出了一种不同的方法,重新实现一个 QComboBox 并创建一个默认项,如果用户单击组合框,则将其删除:

    #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);
    }
    

    然后只需创建此组合框并将项目插入您想要的位置

    MyCombo *combbox = new MyCombo(this);
    combbox->addItem("Option1");
    combbox->addItem("Option2");
    

    【讨论】:

    • 我考虑过这种解决方法。但它仍然有问题。当鼠标按下组合框时,文本“请选择一个选项”将消失。我希望文本“请选择一个选项”始终存在,但用户选择一个选项除外。有没有其他的 qt 控件可以支持这个?谢谢。
    • 所以在用户按下一个选项后,“请选择...”必须返回?
    • 不,如果用户明确选择了一个选项,“请选择一个选项”应该会消失。我希望当下拉列表显示并且用户没有明确选择一个选项时“请选择一个选项”仍然存在,不要选择第一个选项作为默认选项。
    【解决方案3】:

    你可以使用这个库:libqxt

    你可以在这里找到它:https://bitbucket.org/libqxt/libqxt/wiki/Home

    使用对象QxtCheckComboBox,你可以检查你的ComboBox中的多个项目。

    【讨论】:

      【解决方案4】:

      IdlChina 的答案有一个缺点:当您在组合框已经显示时单击它时,它会隐藏并立即再次显示。我有一个稍微不同的方法,没有这个问题。

      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;
      };
      

      【讨论】:

        猜你喜欢
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-21
        • 2015-02-17
        • 2022-07-22
        • 2020-08-19
        • 1970-01-01
        相关资源
        最近更新 更多