詳解 Qt 自動(dòng)完成LineEdit
作者:佚名
本文介紹的是 Qt 自動(dòng)完成LineEdit,一個(gè)錄入界面上有一個(gè)lineEdit,用來錄入代碼進(jìn)行檢索。先來看內(nèi)容。
Qt 自動(dòng)完成LineEdit是本文要介紹的內(nèi)容,內(nèi)容雖少,取其精華。簡單的代碼實(shí)現(xiàn)出很好的效果,先來看內(nèi)容。
CompleteLineEdit.h
- #ifndef COMPLETELINEEDIT_H
- #define COMPLETELINEEDIT_H
- #include <QtGui/QLineEdit>
- #include <QStringList>
- class QListView;
- class QStringListModel;
- class QModelIndex;
- class CompleteLineEdit : public QLineEdit {
- Q_OBJECT
- public:
- CompleteLineEdit(QStringList words, QWidget *parent = 0);
- public slots:
- void setCompleter(const QString &text); // 動(dòng)態(tài)的顯示完成列表
- void completeText(const QModelIndex &index); // 點(diǎn)擊完成列表中的項(xiàng),使用此項(xiàng)自動(dòng)完成輸入的單詞
- protected:
- virtual void keyPressEvent(QKeyEvent *e);
- virtual void focusOutEvent(QFocusEvent *e);
- private:
- QStringList words; // 整個(gè)完成列表的單詞
- QListView *listView; // 完成列表
- QStringListModel *model; // 完成列表的model
- };
- #endif // COMPLETELINEEDIT_H
- CompleteLineEdit.cpp
- #include "CompleteLineEdit.h"
- #include <QKeyEvent>
- #include <QtGui/QListView>
- #include <QtGui/QStringListModel>
- #include <QDebug>
- CompleteLineEdit::CompleteLineEdit(QStringList words, QWidget *parent)
- : QLineEdit(parent), words(words) {
- listView = new QListView(this);
- model = new QStringListModel(this);
- listView->setWindowFlags(Qt::ToolTip);
- connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(setCompleter(const QString &)));
- connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));
- }
- void CompleteLineEdit::focusOutEvent(QFocusEvent *e) {
- //listView->hide();
- }
- void CompleteLineEdit::keyPressEvent(QKeyEvent *e) {
- if (!listView->isHidden()) {
- int key = e->key();
- int count = listView->model()->rowCount();
- QModelIndex currentIndex = listView->currentIndex();
- if (Qt::Key_Down == key) {
- // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中下一個(gè)完成列表中的項(xiàng)
- int row = currentIndex.row() + 1;
- if (row >= count) {
- row = 0;
- }
- QModelIndex index = listView->model()->index(row, 0);
- listView->setCurrentIndex(index);
- } else if (Qt::Key_Up == key) {
- // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中上一個(gè)完成列表中的項(xiàng)
- int row = currentIndex.row() - 1;
- if (row < 0) {
- row = count - 1;
- }
- QModelIndex index = listView->model()->index(row, 0);
- listView->setCurrentIndex(index);
- } else if (Qt::Key_Escape == key) {
- // 按下Esc鍵時(shí),隱藏完成列表
- listView->hide();
- } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {
- // 按下回車鍵時(shí),使用完成列表中選中的項(xiàng),并隱藏完成列表
- if (currentIndex.isValid()) {
- QString text = listView->currentIndex().data().toString();
- setText(text);
- }
- listView->hide();
- } else {
- // 其他情況,隱藏完成列表,并使用QLineEdit的鍵盤按下事件
- listView->hide();
- QLineEdit::keyPressEvent(e);
- }
- } else {
- QLineEdit::keyPressEvent(e);
- }
- }
- void CompleteLineEdit::setCompleter(const QString &text) {
- if (text.isEmpty()) {
- listView->hide();
- return;
- }
- if ((text.length() > 1) && (!listView->isHidden())) {
- return;
- }
- // 如果完整的完成列表中的某個(gè)單詞包含輸入的文本,則加入要顯示的完成列表串中
- QStringList sl;
- foreach(QString word, words) {
- if (word.contains(text)) {
- sl << word;
- }
- }
- model->setStringList(sl);
- listView->setModel(model);
- if (model->rowCount() == 0) {
- return;
- }
- // Position the text edit
- listView->setMinimumWidth(width());
- listView->setMaximumWidth(width());
- QPoint p(0, height());
- int x = mapToGlobal(p).x();
- int y = mapToGlobal(p).y() + 1;
- listView->move(x, y);
- listView->show();
- }
- void CompleteLineEdit::completeText(const QModelIndex &index) {
- QString text = index.data().toString();
- setText(text);
- listView->hide();
- }
- main.cpp
- #include <QtGui/QApplication>
- #include "CompleteLineEdit.h"
- #include <QtGui>
- #include <QCompleter>
- #include <QStringList>
- int main(int argc, char *argv[]) {
- QApplication a(argc, argv);
- QStringList sl = QStringList() << "Biao" << "Bin" << "Huang" << "Hua" << "Hello" << "BinBin" << "Hallo";
- QWidget widgetw;
- CompleteLineEdit * edit= new CompleteLineEdit(sl);
- QPushButton *button = new QPushButton("Button");
- QHBoxLayout *layout = new QHBoxLayout();
- layout->addWidget(edit);
- layout->addWidget(button);
- widgetw.setLayout(layout);
- widgetw.show();
- CompleteLineEdit e(sl);
- e.show();
- return a.exec();
- }
小結(jié):Qt 自動(dòng)完成LineEdit的內(nèi)容介紹介紹完了,效果是不是很滿意,希望本文對(duì)你有所幫助。
責(zé)任編輯:zhaolei
來源:
互聯(lián)網(wǎng)