自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解 Qt 自動(dòng)完成LineEdit

移動(dòng)開發(fā)
本文介紹的是 Qt 自動(dòng)完成LineEdit,一個(gè)錄入界面上有一個(gè)lineEdit,用來錄入代碼進(jìn)行檢索。先來看內(nèi)容。

Qt 自動(dòng)完成LineEdit是本文要介紹的內(nèi)容,內(nèi)容雖少,取其精華。簡單的代碼實(shí)現(xiàn)出很好的效果,先來看內(nèi)容。

詳解 Qt 自動(dòng)完成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); // 動(dòng)態(tài)的顯示完成列表  
  14.     void completeText(const QModelIndex &index); // 點(diǎn)擊完成列表中的項(xiàng),使用此項(xiàng)自動(dòng)完成輸入的單詞  
  15. protected:  
  16.     virtual void keyPressEvent(QKeyEvent *e);  
  17.     virtual void focusOutEvent(QFocusEvent *e);  
  18. private:  
  19.     QStringList words; // 整個(gè)完成列表的單詞  
  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.             // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中下一個(gè)完成列表中的項(xiàng)  
  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.             // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中上一個(gè)完成列表中的項(xiàng)  
  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鍵時(shí),隱藏完成列表  
  68.             listView->hide();  
  69.         } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {  
  70.             // 按下回車鍵時(shí),使用完成列表中選中的項(xiàng),并隱藏完成列表  
  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.     // 如果完整的完成列表中的某個(gè)單詞包含輸入的文本,則加入要顯示的完成列表串中  
  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();  

小結(jié):Qt 自動(dòng)完成LineEdit的內(nèi)容介紹介紹完了,效果是不是很滿意,希望本文對(duì)你有所幫助。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-24 10:05:51

QT 對(duì)象 父對(duì)象

2011-06-28 15:01:01

Qt PIMPL

2011-06-20 17:33:58

Qt MeegoTouch Maemo

2011-06-23 14:05:32

Qt 事件機(jī)制

2021-06-22 09:07:30

QtApple Silic iOS端口

2011-06-17 10:19:11

Qt QWidge QSetting

2011-06-24 10:54:34

Qt Mysql

2011-06-17 09:58:26

Qt Chapter QObject

2011-06-28 16:18:24

Qt QObject

2011-07-04 16:12:00

QT QWidget

2011-07-04 17:18:23

Qt SQLite 數(shù)據(jù)庫

2011-06-24 15:30:22

QT 皮膚 QSS

2011-06-24 14:34:17

Qt 小票 打印

2011-06-30 14:34:17

QT Tablewidge QTableWidg

2011-06-14 11:48:38

Webkit QT

2011-06-20 14:27:57

Qt Embedded

2011-04-02 09:17:38

2009-07-16 09:09:36

ibatis自動(dòng)代碼

2012-04-27 10:00:43

jQuery插件

2011-06-30 09:46:01

QT 顯示視頻 linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)