详解 Qt 自动完成LineEdit

移动开发
本文介绍的是 Qt 自动完成LineEdit,一个录入界面上有一个lineEdit,用来录入代码进行检索。先来看内容。

Qt 自动完成LineEdit是本文要介绍的内容,内容虽少,取其精华。简单的代码实现出很好的效果,先来看内容。

详解 Qt 自动完成LineEdit

CompleteLineEdit.h

  1. #ifndef COMPLETELINEEDIT_H  
  2. #define COMPLETELINEEDIT_H  
  3. #include <QtGui/QLineEdit> 
  4. #include <QStringList> 
  5. class QListView;  
  6. class QStringListModel;  
  7. class QModelIndex;  
  8. class CompleteLineEdit : public QLineEdit {  
  9.     Q_OBJECT  
  10. public:  
  11.     CompleteLineEdit(QStringList words, QWidget *parent = 0);  
  12. public slots:  
  13.     void setCompleter(const QString &text); // 动态的显示完成列表  
  14.     void completeText(const QModelIndex &index); // 点击完成列表中的项,使用此项自动完成输入的单词  
  15. protected:  
  16.     virtual void keyPressEvent(QKeyEvent *e);  
  17.     virtual void focusOutEvent(QFocusEvent *e);  
  18. private:  
  19.     QStringList words; // 整个完成列表的单词  
  20.     QListView *listView; // 完成列表  
  21.     QStringListModel *model; // 完成列表的model  
  22. };  
  23. #endif // COMPLETELINEEDIT_H  
  24.  
  25. CompleteLineEdit.cpp  
  26.  
  27. #include "CompleteLineEdit.h"  
  28. #include <QKeyEvent> 
  29. #include <QtGui/QListView> 
  30. #include <QtGui/QStringListModel> 
  31. #include <QDebug> 
  32. CompleteLineEdit::CompleteLineEdit(QStringList words, QWidget *parent)  
  33.     : QLineEdit(parent), words(words) {  
  34.     listView = new QListView(this);  
  35.     model = new QStringListModel(this);  
  36.     listView->setWindowFlags(Qt::ToolTip);  
  37.     connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(setCompleter(const QString &)));  
  38.     connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));  
  39. }  
  40.  
  41. void CompleteLineEdit::focusOutEvent(QFocusEvent *e) {  
  42.     //listView->hide();  
  43. }  
  44.  
  45. void CompleteLineEdit::keyPressEvent(QKeyEvent *e) {  
  46.     if (!listView->isHidden()) {  
  47.         int key = e->key();  
  48.         int count = listView->model()->rowCount();  
  49.         QModelIndex currentIndex = listView->currentIndex();  
  50.         if (Qt::Key_Down == key) {  
  51.             // 按向下方向键时,移动光标选中下一个完成列表中的项  
  52.             int row = currentIndex.row() + 1;  
  53.             if (row >= count) {  
  54.                 row = 0;  
  55.             }  
  56.             QModelIndex index = listView->model()->index(row, 0);  
  57.             listView->setCurrentIndex(index);  
  58.         } else if (Qt::Key_Up == key) {  
  59.             // 按向下方向键时,移动光标选中上一个完成列表中的项  
  60.             int row = currentIndex.row() - 1;  
  61.             if (row < 0) {  
  62.                 row = count - 1;  
  63.             }  
  64.             QModelIndex index = listView->model()->index(row, 0);  
  65.             listView->setCurrentIndex(index);  
  66.         } else if (Qt::Key_Escape == key) {  
  67.             // 按下Esc键时,隐藏完成列表  
  68.             listView->hide();  
  69.         } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {  
  70.             // 按下回车键时,使用完成列表中选中的项,并隐藏完成列表  
  71.             if (currentIndex.isValid()) {  
  72.                 QString text = listView->currentIndex().data().toString();  
  73.                 setText(text);  
  74.             }  
  75.             listView->hide();  
  76.         } else {  
  77.            // 其他情况,隐藏完成列表,并使用QLineEdit的键盘按下事件  
  78.             listView->hide();  
  79.             QLineEdit::keyPressEvent(e);  
  80.         }  
  81.     } else {  
  82.         QLineEdit::keyPressEvent(e);  
  83.     }  
  84. }  
  85.  
  86. void CompleteLineEdit::setCompleter(const QString &text) {  
  87.     if (text.isEmpty()) {  
  88.         listView->hide();  
  89.         return;  
  90.     }  
  91.     if ((text.length() > 1) && (!listView->isHidden())) {  
  92.         return;  
  93.     }  
  94.     // 如果完整的完成列表中的某个单词包含输入的文本,则加入要显示的完成列表串中  
  95.     QStringList sl;  
  96.     foreach(QString word, words) {  
  97.         if (word.contains(text)) {  
  98.             sl << word;  
  99.         }  
  100.     }  
  101.     model->setStringList(sl);  
  102.     listView->setModel(model);  
  103.     if (model->rowCount() == 0) {  
  104.         return;  
  105.     }  
  106.     // Position the text edit  
  107.     listView->setMinimumWidth(width());  
  108.     listView->setMaximumWidth(width());  
  109.     QPoint p(0, height());  
  110.     int x = mapToGlobal(p).x();  
  111.     int y = mapToGlobal(p).y() + 1;  
  112.     listView->move(x, y);  
  113.     listView->show();  
  114. }  
  115.  
  116. void CompleteLineEdit::completeText(const QModelIndex &index) {  
  117.     QString text = index.data().toString();  
  118.     setText(text);  
  119.     listView->hide();  
  120. }  
  121.  
  122.  
  123. main.cpp  
  124.  
  125. #include <QtGui/QApplication> 
  126. #include "CompleteLineEdit.h"  
  127. #include <QtGui> 
  128. #include <QCompleter> 
  129. #include <QStringList> 
  130. int main(int argc, char *argv[]) {  
  131.     QApplication a(argc, argv);  
  132.     QStringList sl = QStringList() << "Biao" << "Bin" << "Huang" << "Hua" << "Hello" << "BinBin" << "Hallo";  
  133.     QWidget widgetw;  
  134.     CompleteLineEdit * editnew CompleteLineEdit(sl);  
  135.     QPushButton *button = new QPushButton("Button");  
  136.     QHBoxLayout *layout = new QHBoxLayout();  
  137.     layout->addWidget(edit);  
  138.     layout->addWidget(button);  
  139.     widgetw.setLayout(layout);  
  140.     widgetw.show();  
  141.     CompleteLineEdit e(sl);  
  142.     e.show();  
  143.     return a.exec();  

小结:Qt 自动完成LineEdit的内容介绍介绍完了,效果是不是很满意,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-24 10:05:51

QT 对象 父对象

2011-06-20 17:33:58

Qt MeegoTouch Maemo

2011-06-28 15:01:01

Qt PIMPL

2011-06-23 14:05:32

Qt 事件机制

2021-06-22 09:07:30

QtApple Silic iOS端口

2012-04-27 10:00:43

jQuery插件

2009-07-16 09:09:36

ibatis自动代码

2011-06-20 14:27:57

Qt Embedded

2011-06-24 14:34:17

Qt 小票 打印

2011-06-30 14:34:17

QT Tablewidge QTableWidg

2011-04-02 09:17:38

2011-06-14 11:48:38

Webkit QT

2011-06-24 10:54:34

Qt Mysql

2011-07-04 16:12:00

QT QWidget

2011-06-17 10:19:11

Qt QWidge QSetting

2011-06-17 09:58:26

Qt Chapter QObject

2011-07-04 17:18:23

Qt SQLite 数据库

2011-06-24 15:30:22

QT 皮肤 QSS

2011-06-28 16:18:24

Qt QObject

2011-06-30 09:46:01

QT 显示视频 linux
点赞
收藏

51CTO技术栈公众号