-------------------------------------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); // 动态的显示完成列表

void completeText(const QModelIndex &index); // 点击完成列表中的项,使用此项自动完成输入的单词

protected:

virtual void keyPressEvent(QKeyEvent *e);

virtual void focusOutEvent(QFocusEvent *e);

private:

QStringList words; // 整个完成列表的单词

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) {

// 按向下方向键时,移动光标选中下一个完成列表中的项

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) {

// 按向下方向键时,移动光标选中上一个完成列表中的项

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键时,隐藏完成列表

listView->hide();

} else if (Qt::Key_Enter == key || Qt::Key_Return == key) {

// 按下回车键时,使用完成列表中选中的项,并隐藏完成列表

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;

}

// 如果完整的完成列表中的某个单词包含输入的文本,则加入要显示的完成列表串中

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();

}

http://blog.csdn.net/hufengvip/article/details/6555737

QLineEdit 自动完成(使用setCompleter,内含一个ListView)的更多相关文章

  1. Java-集合=第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: List list = new ArrayList(); list.add(new A

    第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得 ...

  2. Android一个ListView列表之中插入两种不同的数据

    http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...

  3. 一个ListView布局的不断演化

    刚出来工作,就负责一个APP的某块功能的编写,该功能就是类似微博那样的界面.微博界面的编写实际上是非常复杂的,虽然它只是一个ListView,但要想让这个ListView滑得动,是的,在一些配置低的手 ...

  4. Android由一个activity 间隔5秒自动跳转到另外一个activity

    Android由一个activity 间隔5秒自动跳转到另外一个activity 2013年10月10日18:03:42 //一.写一个定时器 5秒后开启        final Intent lo ...

  5. 安卓activity之间值共享解决办法,tabhost之间共享父类值,字符串类型的转换,获取每一个listview的item

    1.tabhost父类值共享的解决办法 dianzhanliebiao.java是传值页面,zhuyemian.java放的是tabhost,dianzhangaikuang.java是tabhost ...

  6. android 开发 实现一个ListView套嵌GirdView的滚动布局

    效果图 实现思维: 首先要处理管理好需要导入的数据,我们这里创建class来处理这些数据并且便于管理它们. 创建一个主activity的布局,里面需要一个ListView控件. 创建一个class继承 ...

  7. Android 关于在ScrollView中加上一个ListView,ListView内容显示不完全(总是显示第一项)的问题的两种简单的解决方案

    是这样的哈: 有这样一个需求: 1.显示一个界面,界面上有一个列表(ListView),列表上面有一个可以滚动的海报. 2.要求在ListView滚动的过程中,ListView上面的海报也可以跟着Li ...

  8. 自定义一个ListView实现聊天界面

    摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...

  9. 在ScrollView添加一个ListView造成的滚动问题的简单解决办法()

    正常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把ListView的layout_height改成400dip,而 ...

随机推荐

  1. Offer_1

    #include <iostream> #include <cstring> using namespace std; class CMyString { public: CM ...

  2. PHP - 点击更换头像

    原理: 操作流程: 首先点击头像图片,弹出选择窗口,选中其中一个则窗口推出头像更换. 效果: 主页面代码: <tr> <td>头像:</td> <td> ...

  3. teamviewer无法启动

    在 Linux.Mac OS X和 Windows下都可以用,但在 Linux 下无法启动时怎么办? 笔者回家工作时,都会用 Teamviewer 连线到其他 Linux 桌面,但某天起 Teamvi ...

  4. Ember.js - About

    Ember.js - About More Productive Out of the Box.   Write dramatically less code with Ember's Handleb ...

  5. 【剑指offer】从上向下打印二叉树

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26089165 剑指offer上的第23题,实际上就是考察二叉树的层序遍历,详细思想能够參考 ...

  6. Qt调用摄像头(截取并保存图片)

    原地址:http://blog.csdn.net/liang19890820/article/details/12782531 Qt如何调用系统摄像设备进行显示.截图.录制?     QCamera: ...

  7. Delphi - SEH研究

    技术交流,DH讲解. 前几天一个朋友在弄游戏外挂想带NP调试,就像自己来捕获游戏的异常.好像就要用到SEH这方面的知识.一起研究了一下,这里看下研究 和 在网上找的资料吧.SEH就是Structure ...

  8. Windows Azure入门教学系列 (二):部署第一个Web Role程序

    本文是Windows Azure入门教学的第二篇文章. 在第一篇教学中,我们已经创建了第一个Web Role程序.在这篇教学中,我们将学习如何把该Web Role程序部署到云端. 注意:您需要购买Wi ...

  9. SQL Select语句完整的执行顺序

    1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函数进行计算: 5. 使用having子句筛 ...

  10. shell基础(转)

    shell基础1:文件安全与权限 http://bbs.chinaunix.net/forum/viewtopic.php?t=434579&highlight=wingger 附:Linux ...