qt 自动完成LineEdit

-------------------------------------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 = ); 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() + ;
if (row >= count) {
row = ;
}
QModelIndex index = listView->model()->index(row, );
listView->setCurrentIndex(index); } else if (Qt::Key_Up == key) {
// 按向下方向键时,移动光标选中上一个完成列表中的项
int row = currentIndex.row() - ;
if (row < ) {
row = count - ;
}
QModelIndex index = listView->model()->index(row, );
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() > ) && (!listView->isHidden())) {
return;
} // 如果完整的完成列表中的某个单词包含输入的文本,则加入要显示的完成列表串中
QStringList sl;
foreach(QString word, words) {
if (word.contains(text)) {
sl << word;
}
} model->setStringList(sl);
listView->setModel(model); if (model->rowCount() == ) {
return;
} // Position the text edit
listView->setMinimumWidth(width());
listView->setMaximumWidth(width()); QPoint p(, height());
int x = mapToGlobal(p).x();
int y = mapToGlobal(p).y() + ;
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://www.cnblogs.com/foxhengxing/archive/2010/09/08/1821796.html
qt 自动完成LineEdit的更多相关文章
- C++解析头文件-Qt自动生成信号声明
目录 一.瞎白话 二.背景 三.思路分析 四.代码讲解 1.类图 2.内存结构声明 3.QtHeaderDescription 4.私有函数讲解 五.分析结果 六.下载 一.瞎白话 时间过的ZTMK, ...
- C++解析头文件-Qt自动生成信号定义
目录 一.概述 二.实现思路 三.代码讲解 1.类图 2.QtCppDescription 3.测试 四.源代码 一.概述 上一篇文章C++解析头文件-Qt自动生成信号声明我们主要讲解了怎么去解析C+ ...
- qt 自动重启(两种方法)
所谓自动重启就是程序自动关闭后在重新打开: 一般一个qt程序main函数如下: int main(int argc, char* argv[]) { QApplication app(argc, ar ...
- 使用Qt自动注册Lav
Qt播放视频使用QMediaPlayer要注册Lav解码器,如果手动去注册,每次去使用管理员运行命令或者生成.bat文件都比较麻烦. 解决方法步骤如下: 一:编写注册Lav解码器脚本,并取消控制台的显 ...
- Qt自动生成.rc文件并配置对应属性 程序图标 版本 描述等
Qt项目配置文件pro里需要如下配置,进行qmake,build后会自动生成.rc文件,并将对应的信息写入文件中 VERSION = 1.0.0.1 RC_ICONS = "http.ico ...
- Qt 自动搜索串口号列表
@功能: SerialPortList 类实现当前可用的串口进行实时扫描,当发现有新的串口 或是现有串口消失时,SerialPortList类将发出一个QStringList类型的 信号onNewSe ...
- 如何用Qt自动拷贝exe依赖的dll
QT生成的.exe文件不能运行的解决办法 之前的数独项目的GUI,当我的Qt项目生成exe时,由于缺少了相关的依赖dll文件,打开会一直报缺少依赖文件的错: 然后一开始我到安装的Qt文件夹里把这些有Q ...
- Qt自动填写表单并点击按钮,包括调用js方法
本篇博客参阅了很多其他大牛的文章,具体找不到了,还望包涵>_< 因为其他博客大都是只有主要代码,对于像我这种菜鸟,根本摸不着头脑,以此想总结一下,帮助新手尽快实现功能... 主要是调用了C ...
- qt中的lineEdit文本输入框的输入类型限制(三种验证类)
qt的三种验证类: 1.输入int类型 QValidator *validator=new QIntValidator(100,999,this): QLineEdit *edit=new QLine ...
随机推荐
- Google谷歌搜索引擎登录网站 - Blog透视镜
建置好了网站之后,为了能提升流量或是增加曝光度,Mix通常会到Google谷歌,用手动登录的方式,登录网站,不久之后,搜索引擎就会派遣蜘蛛机器人,来检索你的网站,等一段时间之后,就会出现在搜索引擎内, ...
- jquery validationEngine的使用
1.引入文件 <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></s ...
- 学习DSP(三)安装C2833x/C2823x C/C++ 头文件和外设示例-压缩包
进入http://www.ti.com.cn/product/cn/tms320f28335 下载C2833x/C2823x C/C++ 头文件和外设示例 即SPRC530,目前最新版本是V131.安 ...
- Spring MVC对象转换说明
在Spring MVC之前我们需要在Servlet里处理HttpServletRequest参数对象,但这个对象里的属性都是通用类型的对象(如字符串),处理起来很繁琐并且容易出错,而Spring MV ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
- php 原理相关
[PHP 运行方式(PHP SAPI介绍)] http://www.phpddt.com/php/php-sapi.html [PHP内核探索:从SAPI接口开始]http://www.nowamag ...
- 改变UITableViewCell按下去的颜色
UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...
- [工作问题总结]MyEclipse 注册
------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------ 1.本人 ...
- android:强大的图片下载和缓存库Picasso
1.Picasso简单介绍 Picasso是Square公司出品的一个强大的图片下载和缓存图片库.官方网址是:http://square.github.io/picasso/ 仅仅须要一句代码就能够将 ...
- WPF拖动总结[转载]
WPF拖动总结 这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...