美文网首页
QlineEdit下拉列表设置及无文字时鼠标点击显示所有下拉数据

QlineEdit下拉列表设置及无文字时鼠标点击显示所有下拉数据

作者: 可不期诺Cappuccino | 来源:发表于2019-11-14 14:24 被阅读0次

    1.增加如下设置

    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
    lineEdit->completer()->complete();

    这是你如何做到的:

    为你的lineedit定义textChanged插槽;
    覆盖窗口的customEvent方法;
    在textChanged插槽中将用户事件发送到窗口
    lineedit的文字长度为零;
    在customEvent方法中,只要收到用户事件,就会显示完成;

    以下是一个例子:

    mainwindow.h:

    1. class MainWindow : public QMainWindow

    2. {

    3. Q_OBJECT

    4. public:

    5. explicit MainWindow(QWidget *parent = 0);

    6. ~MainWindow();

    7. protected:

    8. void customEvent(QEvent * event);

    9. private:

    10. Ui::MainWindow *ui;

    11. private slots:

    12. void on_lineEdit_textChanged(QString );

    13. };

    mainwindow.cpp:

    1. class CompleteEvent : public QEvent

    2. {

    3. public:

    4. CompleteEvent(QLineEdit *lineEdit) : QEvent(QEvent::User),m_lineEdit(lineEdit) { }

    5. void complete()

    6. {

    7. m_lineEdit->completer()->complete();

    8. }

    9. private:

    10. QLineEdit *m_lineEdit;

    11. };

    12. MainWindow::MainWindow(QWidget *parent) :

    13. QMainWindow(parent),ui(new Ui::MainWindow)

    14. {

    15. ui->setupUi(this);

    16. QStringList wordList;

    17. wordList << "one" << "two" << "three" << "four";

    18. QLineEdit *lineEdit = new QLineEdit(this);

    19. lineEdit->setGeometry(20,20,200,30);

    20. connect(lineEdit,SIGNAL(textChanged(QString)),SLOT(on_lineEdit_textChanged(QString )));

    21. QCompleter *completer = new QCompleter(wordList,this);

    22. completer->setCaseSensitivity(Qt::CaseInsensitive);

    23. completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);

    24. lineEdit->setCompleter(completer);

    25. }

    26. MainWindow::~MainWindow()

    27. {

    28. delete ui;

    29. }

    30. void MainWindow::customEvent(QEvent * event)

    31. {

    32. QMainWindow::customEvent(event);

    33. if (event->type()==QEvent::User)

    34. ((CompleteEvent*)event)->complete();

    35. }

    36. void MainWindow::on_lineEdit_textChanged(QString text)

    37. {

    38. if (text.length()==0)

    39. QApplication::postEvent(this,new CompleteEvent((QLineEdit*)sender()));

    40. }

    相关文章

      网友评论

          本文标题:QlineEdit下拉列表设置及无文字时鼠标点击显示所有下拉数据

          本文链接:https://www.haomeiwen.com/subject/okjrictx.html