-------------------------------------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. HEVC码率控制浅析——HM代码阅读之一

    HM的码率控制提案主要参考如下三篇:K0103,M0036,M0257.本文及后续文章将基于HM12.0进行讨论,且首先仅讨论K0103对应的代码,之后再陆续补充M0036,M0257对应的代码分析, ...

  2. perl 读取cookie

    use LWP::UserAgent; use HTTP::Date qw(time2iso str2time time2iso time2isoz); use Net::Ping; use Sock ...

  3. 一步一步重写 CodeIgniter 框架 -- 原因和思路

    CodeIgniter 是一个非常轻量级的 PHP 框架,说是轻量级,最新版的代码只有不到2M. 其最重要的特点就是 MVC 模式来编写代码,如果大家看过一些用 PHP 来编写网站的书籍或教程,无一例 ...

  4. UVA 10581 - Partitioning for fun and profit(数论递推)

    10581 - Partitioning for fun and profit 题目链接 题意:给定m, n,表示分配给n个格子,分配m个数字进去,每一个格子最少1,而且序列要是递增的,问第k个字典序 ...

  5. C#获取千分位,给数字加逗号分隔符

    /// <summary> /// 对数字添加”,“号,可以处理负数以及带有小数的情况 /// </summary> /// <param name="vers ...

  6. WPF DataGrid 增加"更新"模板列,根据行Row的选择而显示"更新"按钮

    SelectionMode="Single" <DataGridTemplateColumn Header=""> <DataGridTemp ...

  7. 基于visual Studio2013解决面试题之1309求子集

     题目

  8. Swift - 使用Media Player播放本地视频,在线视频

    Media Player框架用于播放本地视频.音频,也可以在线播放视频和音频. 1,播放器MPMovieControlStyle样式有如下几种: (1)None: 没有播放控制控件 (2)Embedd ...

  9. Linux下mpi环境配置与执行步骤(Ubuntu为例)

    转载注明出处: http://blog.csdn.net/bendanban/article/details/9136755 以两台计算机为例,将这两台计算机应用于MPI运行环境. 第一步:在两台机器 ...

  10. GDI GDI+ 的区别

    GDI+是GDI的下一个版本,它进行了很好的改进,并且易用性更好.GDI的一个好处就是你不必知道任何关于数据怎样在设备上渲染的细节,GDI+更好的实现了这个优点,也就是说,GDI是一个中低层API,你 ...