#includeclassQListView;classQStringListModel;class" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

QLineEdit 自動(dòng)完成

系統(tǒng) 2778 0
-------------------------------------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();

}

QLineEdit 自動(dòng)完成


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 91啦视频在线观看 | 久久99精品久久久久久黑人 | 久久久久久久国产高清 | 国产一区二区在线视频 | 狠狠色噜噜狠狠狠狠奇米777 | 奇米影视第四色777 奇米影视第四色7777 | 99婷婷 | 五月天亚洲视频 | 国产成+人欧美+综合在线观看 | 亚洲精品影视 | 日韩午夜在线视频 | 欧美 日韩 中字 国产 | 国产成人一区二区三区免费观看 | 色婷婷5月精品久久久久 | 九九干| 午夜在线网址 | 国产精品美女视频 | 99精品视频在线这里只有 | 手机看片欧美日韩 | 天天天做天天天天爱天天想 | 久久爆操 | chinese国产xxxx中国| 欧洲欧美成人免费大片 | 久久这里只有精品9 | 成人毛片免费观看视频在线 | 欧美深度肠交惨叫 | 奇米影视第四色888 奇米影视第四色首页 | 337p日本大胆欧美人术艺术6 | 成人精品网 | 天天综合天天 | jizzjizz欧美69巨大 | 五月激情六月婷婷 | 免费区欧美一级毛片精品 | 亚洲第一在线 | 亚洲精品久久久久福利网站 | 中文字幕不卡免费高清视频 | 亚洲综合色就色手机在线观看 | 天天爽天天| 欧美激情亚洲精品日韩1区2区 | 二区三区 | 黄wwwwww|