-------------------------------------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的更多相关文章

  1. C++解析头文件-Qt自动生成信号声明

    目录 一.瞎白话 二.背景 三.思路分析 四.代码讲解 1.类图 2.内存结构声明 3.QtHeaderDescription 4.私有函数讲解 五.分析结果 六.下载 一.瞎白话 时间过的ZTMK, ...

  2. C++解析头文件-Qt自动生成信号定义

    目录 一.概述 二.实现思路 三.代码讲解 1.类图 2.QtCppDescription 3.测试 四.源代码 一.概述 上一篇文章C++解析头文件-Qt自动生成信号声明我们主要讲解了怎么去解析C+ ...

  3. qt 自动重启(两种方法)

    所谓自动重启就是程序自动关闭后在重新打开: 一般一个qt程序main函数如下: int main(int argc, char* argv[]) { QApplication app(argc, ar ...

  4. 使用Qt自动注册Lav

    Qt播放视频使用QMediaPlayer要注册Lav解码器,如果手动去注册,每次去使用管理员运行命令或者生成.bat文件都比较麻烦. 解决方法步骤如下: 一:编写注册Lav解码器脚本,并取消控制台的显 ...

  5. Qt自动生成.rc文件并配置对应属性 程序图标 版本 描述等

    Qt项目配置文件pro里需要如下配置,进行qmake,build后会自动生成.rc文件,并将对应的信息写入文件中 VERSION = 1.0.0.1 RC_ICONS = "http.ico ...

  6. Qt 自动搜索串口号列表

    @功能: SerialPortList 类实现当前可用的串口进行实时扫描,当发现有新的串口 或是现有串口消失时,SerialPortList类将发出一个QStringList类型的 信号onNewSe ...

  7. 如何用Qt自动拷贝exe依赖的dll

    QT生成的.exe文件不能运行的解决办法 之前的数独项目的GUI,当我的Qt项目生成exe时,由于缺少了相关的依赖dll文件,打开会一直报缺少依赖文件的错: 然后一开始我到安装的Qt文件夹里把这些有Q ...

  8. Qt自动填写表单并点击按钮,包括调用js方法

    本篇博客参阅了很多其他大牛的文章,具体找不到了,还望包涵>_< 因为其他博客大都是只有主要代码,对于像我这种菜鸟,根本摸不着头脑,以此想总结一下,帮助新手尽快实现功能... 主要是调用了C ...

  9. qt中的lineEdit文本输入框的输入类型限制(三种验证类)

    qt的三种验证类: 1.输入int类型 QValidator *validator=new QIntValidator(100,999,this): QLineEdit *edit=new QLine ...

随机推荐

  1. FreeBSD 安装axel提高ports的安装速度

    ########################  FreeBSD安装Ports ######################## 1 # ee /etc/portsnap.conf 设置SERVER ...

  2. TEA加密

    TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,支持128位密码,与BlowFish一样TEA每次只能加密/解密8字节数据.TEA特点是速度快.效率高,实现也 ...

  3. C 宏定义

    C/C++中宏使用总结 .C/C++中宏总结C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释 ...

  4. SQL Serverf 索引 - 索引压缩 、附加特性 <第十篇>

    一.索引压缩 数据和索引压缩在SQL Server2008被引入.压缩一个索引意味着将在一个页面中获得更多的关键字信息.这可以造成重大的性能改进,因为存储索引需要的页面和索引级别更少.因为索引中的键值 ...

  5. 软件体系结构经典问题——KWIC的分析和解决

    KWIC作为一个早年间在ACM的Paper提出的一个问题,被全世界各个大学的软件设计课程奉为课堂讲义或者作业的经典.(From Wiki,FYI,D. L. Parnas uses a KWIC In ...

  6. javaWeb Cache技术――OSCache(转-全)

    什么是osCache? 它是:http://baike.baidu.com/view/1835163.htm?fr=aladdin OSCache使用指南 一.下载安装 OSCache是一个基于web ...

  7. hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)

    系统:ubuntu14.04 一.hadoop安装 ssh免密码登陆详情见上一篇博客. 解压hadoop1.2.1到某个目录下,这里解压到ubuntu下载目录下(注意没必要使用管理员权限) 在hado ...

  8. C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0

    Generating Files with the TextTransform Utility \Program Files\Common Files\Microsoft Shared\TextTem ...

  9. 【转】【漫画解读】HDFS存储原理

    根据Maneesh Varshney的漫画改编,以简洁易懂的漫画形式讲解HDFS存储机制与运行原理. 一.角色出演 如上图所示,HDFS存储相关角色与功能如下: Client:客户端,系统使用者,调用 ...

  10. 代码中实际运用memcached——java

    以下文章取自:http://jameswxx.iteye.com/blog/1168711 memcached的java客户端有好几种,http://code.google.com/p/memcach ...