【问题标题】:A QLineEdit with a QCompleter won't show the QCompleter's popup menu with an empty text field带有 QCompleter 的 QLineEdit 不会显示带有空文本字段的 QCompleter 弹出菜单
【发布时间】:2011-01-31 23:08:51
【问题描述】:

我有一个QLineEdit,有一个与之关联的QCompleter 对象。如果用户输入了至少一个字符,则会显示来自QCompleter 的弹出菜单,但是当用户删除最后一个字符(从而使该字段为空)时,弹出菜单就会消失。即使QLineEdit的文字为空,有什么办法让它显示?

【问题讨论】:

    标签: c++ qt qt4


    【解决方案1】:

    一旦你的行编辑文本被使用QCompleter::complete槽删除,你应该能够强制显示完成者的弹出窗口:

    lineEdit->completer()->complete();
    

    你可以这样做:

    • 为您的 lineedit 定义 textChanged 槽;
    • 为您的窗口覆盖 customEvent 方法;
    • 在 textChanged 槽中,无论何时,都将用户事件发送到窗口 lineedit 的文本长度为零;
    • 在 customEvent 方法中,每当收到用户事件时显示完成者;

    下面是一个例子:

    主窗口.h:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    protected:
        void customEvent(QEvent * event);
    
    private:
        Ui::MainWindow *ui;
    
    private slots:
        void on_lineEdit_textChanged(QString );
    };
    

    主窗口.cpp:

    class CompleteEvent : public QEvent
    {
    public:
        CompleteEvent(QLineEdit *lineEdit) : QEvent(QEvent::User), m_lineEdit(lineEdit) { }
    
        void complete()
        {
            m_lineEdit->completer()->complete();
        }
    
    private:
        QLineEdit *m_lineEdit;
    };
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QStringList wordList;
        wordList << "one" << "two" << "three" << "four";
    
        QLineEdit *lineEdit = new QLineEdit(this);
        lineEdit->setGeometry(20, 20, 200, 30);
        connect(lineEdit, SIGNAL(textChanged(QString)), SLOT(on_lineEdit_textChanged(QString )));
    
        QCompleter *completer = new QCompleter(wordList, this);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
        lineEdit->setCompleter(completer);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::customEvent(QEvent * event)
    {
        QMainWindow::customEvent(event);
        if (event->type()==QEvent::User)
            ((CompleteEvent*)event)->complete();
    }
    
    void MainWindow::on_lineEdit_textChanged(QString text)
    {
        if (text.length()==0)
            QApplication::postEvent(this, new CompleteEvent((QLineEdit*)sender()));
    }
    

    希望这会有所帮助,问候

    【讨论】:

    • 像魅力一样工作,谢谢!我试图用 QTimer::singleShot 做类似的事情,但由于某种原因它不起作用。
    【解决方案2】:

    这是我基于 serge_gubenko 回答的解决方案。该类使用 QStringListModel,但可以很容易地替换为任何其他模型。

    Completer_line_edit.h

    #include <QLineEdit>
    #include <QStringListModel>
    #include <QTimer>
    
    /*! Line edit widget with auto completion based on QStringListModel.
      Modified behaviour: completion list will appear even when contents of
      line edit is empty. Full list of options will be showed when line edit
      has focus and is empty.
      */
    class Completer_line_edit : public QLineEdit {
      Q_OBJECT
    public:
      explicit Completer_line_edit(QWidget *parent = 0);
    
      //! Set list of options used for copmletion.
      inline void set_list(QStringList list) { model.setStringList(list); }
    
    private:
      QStringListModel model;
      void focusInEvent(QFocusEvent *e);
      void customEvent(QEvent* e);
      QTimer timer;
    
    private slots:
      void slot_text_edited();
      void slot_call_popup();
    
    };
    

    Completer_line_edit.cpp

    #include "Completer_line_edit.h"
    #include <QCompleter>
    #include <QEvent>
    #include <QApplication>
    
    Completer_line_edit::Completer_line_edit(QWidget *parent) :
      QLineEdit(parent)
    {
      setCompleter(new QCompleter());
      completer()->setModel(&model);
      completer()->setCompletionMode(QCompleter::PopupCompletion);
      completer()->setCaseSensitivity(Qt::CaseInsensitive);
      connect(this, SIGNAL(textEdited(QString)), this, SLOT(slot_text_edited()));
    }
    
    void Completer_line_edit::focusInEvent(QFocusEvent *e) {
      QLineEdit::focusInEvent(e);
      // force completion when line edit is focued in
      completer()->complete();
    }
    
    void Completer_line_edit::slot_text_edited() {
      qDebug() << "text edited";
      // force to show all items when text is empty
      completer()->setCompletionMode(text().isEmpty()? QCompleter::UnfilteredPopupCompletion: QCompleter::PopupCompletion);
      if (text().isEmpty()) {
        // completion list will be hidden now; we will show it again after a delay
        timer.singleShot(100, this, SLOT(slot_call_popup()));
      }
    }
    
    void Completer_line_edit::slot_call_popup() {
      // apparently, complete() works only in event handler
      QApplication::postEvent(this, new QEvent(QEvent::User));
    }
    
    void Completer_line_edit::customEvent(QEvent *e) {
      QLineEdit::customEvent(e);
      // force completion after text is deleted
      completer()->complete();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多