自动完成的QLineEdit(非使用QCompleter版) 收藏_qlineedit qcompleter 按2次向下箭头 才能选择-程序员宅基地

技术标签: button  layout  qt  listview  signal  class  

自动完成的QLineEdit(非使用QCompleter版) 收藏


-------------------------------------CompleteLineEdit.h-------------------------------------
view plaincopy to clipboardprint?
#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 
#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-------------------------------------
view plaincopy to clipboardprint?
#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 "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----------------------------------
view plaincopy to clipboardprint?
#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/starcloud_zxt/archive/2010/01/13/5186489.aspx

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhh_steven/article/details/5846818

智能推荐

JAVA微信扫码支付模式二功能实现完整例子_java微信支付v2示例-程序员宅基地

文章浏览阅读3.4k次。概述本例子实现微信扫码支付模式二的支付功能,应用场景是,web网站微信扫码支付。实现从点击付费按钮、到弹出二维码、到用户用手机微信扫码支付、到手机上用户付费成功、web网页再自动调整到支付成功后的页面,这一个过程。详细一、准备工作先开通微信公众号,再开通微信公众号里面的微信支付功能,这些是前提条件,多说一句,申请开通微信公众号需要等待审核_java微信支付v2示例

virt-manager 解决 kvm虚拟机中鼠标不同步的问题_kvm虚拟机卡,鼠标不能移动到状态栏-程序员宅基地

文章浏览阅读6.1k次。今天在VNC界面中感觉virt-manager管理的虚拟机界面总是鼠标跟不上,指到哪儿也看不出来,界面上一直显示press control_l+a/t_l来移动鼠标,想要修改鼠标和宿主机界面同步方法如下: cd /etc/libvirt/qemu; vi 修改相应的xml文件; 在标签下添加 ; 最后再 virsh define /etc/libvirt/qemu/**.xml(相应的xm_kvm虚拟机卡,鼠标不能移动到状态栏

js 正则过滤和增加富文本中图片前缀_正则处理图片和文字js-程序员宅基地

文章浏览阅读1.2k次。富文本中过滤和增加图片前缀_正则处理图片和文字js

vue使用fetch发送json格式的post请求报错解决方法_fetch()发送接口报错 vue3-程序员宅基地

文章浏览阅读4.2k次,点赞9次,收藏7次。学习vue的fetch发送post的json格式数据请求时发生了跨域报错,自己解决不了,常规操作上网看博客,找了很久,都是说在后台服务器设置跨域允许,但是我明明设置了呀,我看的黑马教程,明明和视频一毛一样了还是不行…一脸问号,终于在找了很久后迎来了曙光…废话不多说…前台发送请求的代码:fetch('http://localhost:3000/books', { method: 'post', body: JSON.stringify({ _fetch()发送接口报错 vue3

fabric1.1 ca集成_fabric ca enroll register-程序员宅基地

文章浏览阅读688次。目录1.初始化ca服务2.修改配置文件fabric-ca-server-config.yaml3.启动fabric-ca-server4.enroll管理员账号5.register一个普通角色账号6.enroll账号7.复制管理员签名和公用TLS证书文件8.将usertest用户的msp文件和tls文件复制到org1下9.检验usertest身份是否可用..._fabric ca enroll register

黑客搜索大法(Google Hacking)_index of /password-程序员宅基地

文章浏览阅读7.9k次,点赞6次,收藏36次。目录前言逻辑连接符搜索语法index of前言Google Hacking 是利用谷歌搜索在浩瀚的互联网中搜索到我们需要的信息。轻量级的搜索可以搜素出一些遗留后门,不想被发现的后台入口,中量级的搜索可以搜索出一些用户信息泄露,源代码泄露,未授权访问等等,重量级的则可能是mdb文件下载,CMS 未被锁定install页面,网站配置密码,php远程文件包含漏洞等重要信息。逻辑连接符· 逻辑与:..._index of /password

随便推点

S_ISREG S_ISREG stat 路径判断常见宏-程序员宅基地

文章浏览阅读431次。参考链接:https://blog.csdn.net/lj19990824/article/details/120047026。使用stat结构结合常用宏判断一个地址字符串是否是路径或者常规文件。/*常规文件判断*/_s_isreg

sql注入_and 1=1-程序员宅基地

文章浏览阅读3.2k次。文章目录一些简单的判断注入点的方法:一些简单的判断注入点的方法:1.单引号判断http://www.xxx.com/xxx.asp?id=10’ 如果出现错误提示,则该网站可能就存在注入漏洞。2.and判断http://www.xxx.com/xxx.asp?id=10’and 1=1这个条件永远都是真的,所以当然返回是正常页http://www.xxx.com/xxx.asp?id=10’and 1=2如果报错那说明存在注入漏洞,还要看报的什么错,不可能报任何错都有注入漏洞的。3.or判断(_and 1=1

adb 'grep' 不是内部或外部命令,也不是可运行的程序或批处理文件_adb 'grep' 不是内部或外部命令,也不是可运行的程序 或批处理文件。-程序员宅基地

文章浏览阅读1.8k次。出现问题时的命令: adb shell logcat | grep START问题内容:’grep’ 不是内部或外部命令,也不是可运行的程序或批处理文件。解决方法:将logcat | grep START用双引号引起来,即adb shell “logcat | grep START”。..._adb 'grep' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

vue使用 echarts 3d echarts_esm_echarts__WEBPACK_IMPORTED_MODULE_0__.registerPostInit is not a function_i.registerpostinit is not a function-程序员宅基地

文章浏览阅读5.2k次,点赞4次,收藏3次。查看vue中echarts 和echarts-gl版本卸载 当前echarts和echarts-glnpm uninstall echarts-gl npm uninstall echarts 这里echarts有个版本为4.0.1npm install --save [email protected]//再安装3dnpm install --save echarts-gl 启动vue..._i.registerpostinit is not a function

语音信号的预处理-程序员宅基地

文章浏览阅读6k次,点赞2次,收藏47次。概述语音信号是一种非平稳的时变信号,它携带着大量信息。在语音编码、语音合成、语音识别和语音增强等语音处理中,都需要提取语音中包含的各种信息语音处理的目的对语音信号进行分析,提取特征参数,用于后续处理加工语音信息,如语音增强和语音合成中的应用根据所分析的参数类型,语音信号可以分成:时域分析最简单、最直观直接对语音信号的时域波形进行分析特征参数:语音的短时能力、平均幅度、短时..._语音信号的预处理

node-xlsx合并单元格_node-xlsx 合并单元格-程序员宅基地

文章浏览阅读6.2k次。使用 node-xlsx 合并单元格方式const data = [['1','2','3'],['4','5','6']]const range0 = {s: {c: 0, r:0 },e: {c:0, r:1}}; //此处是合并条件 0,0和0,1是坐标 指的是A1单元格 到A2单元合并const options = {'!merges': [ range0]};//如..._node-xlsx 合并单元格

推荐文章

热门文章

相关标签