QT 常用部件 (四)自动完成LineEdit

来源:互联网 发布:光信息科学与技术知乎 编辑:程序博客网 时间:2024/05/02 00:12

http://mobile.51cto.com/symbian-271180.htm

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的内容介绍介绍完了,效果是不是很满意,希望本文对你有所帮助。

原创粉丝点击