QLineEdit 自动完成(使用setCompleter,内含一个ListView)
#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
#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();
}
#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)的更多相关文章
- 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; 要求完善设计,使得 ...
- Android一个ListView列表之中插入两种不同的数据
http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...
- 一个ListView布局的不断演化
刚出来工作,就负责一个APP的某块功能的编写,该功能就是类似微博那样的界面.微博界面的编写实际上是非常复杂的,虽然它只是一个ListView,但要想让这个ListView滑得动,是的,在一些配置低的手 ...
- Android由一个activity 间隔5秒自动跳转到另外一个activity
Android由一个activity 间隔5秒自动跳转到另外一个activity 2013年10月10日18:03:42 //一.写一个定时器 5秒后开启 final Intent lo ...
- 安卓activity之间值共享解决办法,tabhost之间共享父类值,字符串类型的转换,获取每一个listview的item
1.tabhost父类值共享的解决办法 dianzhanliebiao.java是传值页面,zhuyemian.java放的是tabhost,dianzhangaikuang.java是tabhost ...
- android 开发 实现一个ListView套嵌GirdView的滚动布局
效果图 实现思维: 首先要处理管理好需要导入的数据,我们这里创建class来处理这些数据并且便于管理它们. 创建一个主activity的布局,里面需要一个ListView控件. 创建一个class继承 ...
- Android 关于在ScrollView中加上一个ListView,ListView内容显示不完全(总是显示第一项)的问题的两种简单的解决方案
是这样的哈: 有这样一个需求: 1.显示一个界面,界面上有一个列表(ListView),列表上面有一个可以滚动的海报. 2.要求在ListView滚动的过程中,ListView上面的海报也可以跟着Li ...
- 自定义一个ListView实现聊天界面
摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...
- 在ScrollView添加一个ListView造成的滚动问题的简单解决办法()
正常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把ListView的layout_height改成400dip,而 ...
随机推荐
- android APP 中微信分享功能实现 的总结
//花了很长时间最终完成了微信分享功能,中间走了很多弯路,在此做一下小结,希望对在应用中使用到微信分享的朋友有所帮助. 主要问题就是下面两个: 1.为什么运行了项目之后,微信分享只是闪了一下就没有了? ...
- jquery遍历筛选数组的几种方法和遍历解析json对象
jquery grep()筛选遍历数组 $().ready( function(){ var array = [1,2,3,4,5,6,7,8,9]; var fil ...
- ibatis通过Map封装参数调用存储过程
一.存储过程如下(领导写的) CREATE OR REPLACE PROCEDURE agent_UIMOrIMEICheck_pro ( I_CARD_NO IN VARCHAR2, --UIM卡或 ...
- android 播放音乐-进度条
今天学渣研究了一下使用MediaPlayer播放音乐时加入进度条,进度条如今用的是android自带的seekbar,后期会跟换UI的,在之前可以播放音乐的基础上,如今加入的主要功能有两个: 1实时显 ...
- gdi+ 高速绘制透明窗体
gdi+ 高速绘制透明窗体: 方法一: 1.用Iamge对象载入png资源, 2.调用drawimage函数讲图片绘制出了 3.UpdateLayeredWindow对窗体进行布局 方法二: 1.用B ...
- Python的字符串格式化符号
1.字符串格式化符号含义 %c:格式化字符及其ASCII码 %s:格式化字符串 %d:格式化整数 %o:格式化无符号八进制数 %x:格式化无符号十六进制数 %X:格式化无符号十六进制数(大写) %f: ...
- Socket开发时,Available为0,实际还有数据的问题
这段时间处理Socket通讯,比如文件传输,通常代码如下: string filename = @"c:\abc.txt"; // 发送文件名字符串长度(测试代码,实际请传输字符串 ...
- .net面试题大全(有答案)
在网上找来的,希望对大家有所帮助. 1 (1)面向对象的语言具有__继承性_性._封装性_性._多态性 性. (2)能用foreach遍历访问的对象需要实现 _ IEnumerable 接口或 ...
- C#分析URL参数获取参数和值得对应列表
原文: C#分析URL参数获取参数和值得对应列表 /// <summary> /// 分析url链接,返回参数集合 /// </summary> /// <param n ...
- 【ASP.NET Web API教程】1 ASP.NET Web API入门
原文 [ASP.NET Web API教程]1 ASP.NET Web API入门 Getting Started with ASP.NET Web API第1章 ASP.NET Web API入门 ...