当前位置:网站首页>QCompleter的进阶使用
QCompleter的进阶使用
2022-08-06 05:26:00 【MStudyStudio】
1. 获取QCompleter的生成的匹配结果
#include <QtWidgets/QCompleter>
#include <QtCore/QStringListModel>
#include <QtCore/QAbstractProxyModel>
QStringList searchList({
"海","海龟","海底捞","海胆","火","大火","风火轮","海风"});
QCompleter c;
//模型填入父亲 c 后,completer searchList,后面替换不用主动析构
c.setModel(new QStringListModel(searchList,&c));
c.setCompletionMode(QCompleter::PopupCompletion);
c.setCaseSensitivity(Qt::CaseInsensitive);
c.setCompletionPrefix("海"); //搜索前缀
QAbstractProxyModel *completionModel = dynamic_cast<QAbstractProxyModel*>(c.completionModel());
int nCompletionCount = completionModel->rowCount();
QStringList searchRet;
for(int i=0 ; i<nCompletionCount ; ++i){
QModelIndex proxyIndex = completionModel->index(i,0);
QModelIndex completeIndex = completionModel->mapToSource(proxyIndex);
searchRet.push_back(completeIndex.data().toString());
}
2. 在内置的QLineEdit中使用QCompleter
2.1如何监听QCompleter按下的案件,如回车按键等
需要对lineedit的popup 安装事件过滤器
class CLineEdit : public QLineEdit{
Q_OBJECT
public:
CLineEdit(QWidget* parent = nullptr): QLineEdit(parent){
}
~CLineEdit(){
}
private:
bool eventFilter(QObject *obj, QEvent *event) override{
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
if(keyEvent && ( keyEvent->key() == Qt::Key_Enter ||keyEvent->key() == Qt::Key_Return)){
return true;
}
}
return QObject::eventFilter(obj, event);
}
};
CLineEdit lineEdit;
lineEdit.setCompleter(new QCompleter);
lineEdit.completer()->popup()->installEventFilter(&lineEdit);
边栏推荐
- 如何根据页面标签自动生成文章目录?分析+代码详解
- 十四、一起学习Lua 元表(Metatable)
- 十一、一起学习Lua 迭代器
- 旷视提出PETRv2:统一的多摄像头3D感知框架
- MySQL复习
- Kindle access to HomeAssistant: Realize lock screen wallpaper to display device information in HA and obtain Kindle power in HA
- Qt智能指针
- After the Xiaomi AX3600 router is expanded and flashed OpenWrt, how to flash back to the official system and partition
- 【多传感器融合】卡尔曼滤波
- 7月17日上午,阿里AE技术团队直播专场,分享CVPR挑战赛冠军、亚军方案!
猜你喜欢
随机推荐
十四、一起学习Lua 元表(Metatable)
SemEval 2022 | 将知识引入NER系统,阿里达摩院获最佳论文奖
R爬虫常用的包与用法
Responsive layout
Class类对象实例化,newInstance方法,工厂+反射方法,使用反射调用构造方法,反射调用方法,反射调用成员
(上)苹果有开源,但又怎样呢?
全网最全面的SSM整合(没有之一)
动态规划之不同路径 II
Getting to know the network layer
rsync+inotify远程同步
获取arcgis server 发布的mapserver图例
动态规划之粉刷房子
旷视提出PETRv2:统一的多摄像头3D感知框架
统计字符串中英文字母、空格、数字和其它字符的个数.读取文件中的学生信息,计算出考试的平均分。创建一个Student类的数组,封装5个学生的信息,按指定条件查找学生.计算当前时间距明年春节还有多长时间。
科普:String hashCode 方法为什么选择数字 31 作为乘子
sql多表查询,嵌套查询,函数查询
Redis三种模式——主从复制,哨兵模式,集群
集合应用,产生10000个0-9之间的随机数字,统计每个数字出现的个数。
Qt智能指针
分层架构&SOA架构









