Qt 之自定义搜索框QLineEdit
Qt 之自定义搜索框
2015年12月19日 10:44:14 一去丶二三里 阅读数 15126更多
分类专栏: Qt 《Qt 实战一二三》
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011012932/article/details/50357523
简述
关于搜索框,大家都经常接触。例如:浏览器搜索、Windows资源管理器搜索等。
当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定。
方案一:调用QLineEdit现有接口
void addAction(QAction * action, ActionPosition position)
在QLineEdit的前/后添加部件,ActionPosition表示部件所在方位。QAction * addAction(const QIcon & icon, ActionPosition position)
重载函数。
枚举:QLineEdit::ActionPosition
常量 | 值 | 描述 |
---|---|---|
QLineEdit::LeadingPosition | 0 | 当使用布局方向Qt::LeftToRight时,部件显示在文本左侧,使用Qt::RightToLeft则显示在右侧。 |
QLineEdit::TrailingPosition | 1 | 当使用布局方向Qt::LeftToRight时,部件显示在文本右侧,使用Qt::RightToLeft则显示在左侧。 |
- 方案二:自定义(可以实现任何组合)
下面,我们来针对自定义进行讲解。
| 版权声明:一去、二三里,未经博主允许不得转载。
效果
细节分析
实现细节需要如下步骤:
- 组合实现,输入框+按钮
- 事件关联
- 获取输入文本,进行文本搜索
为了更人性、易用,这里有一些细节需要注意:
- 输入框的文本不能处于按钮之下
- 输入框无文本时必须给与友好性提示
- 按钮无文本描述,一般需要给予ToolTip提示
- 按钮样式-正常、滑过、按下,以及鼠标滑过鼠标样式手型,
这些都想清楚了,我们就能快速实现一个搜索框了。
Coding
搜索框实现
<span style="color:#000000"><code class="language-Qt">m_pSearchLineEdit <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QLineEdit();
QPushButton <span style="color:#4f4f4f !important">*</span>pSearchButton <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QPushButton(this);pSearchButton<span style="color:#4f4f4f !important">-></span>setCursor(Qt<span style="color:#006666 !important">::PointingHandCursor</span>);
pSearchButton<span style="color:#4f4f4f !important">-></span>setFixedSize(<span style="color:#006666 !important">22</span>, <span style="color:#006666 !important">22</span>);
pSearchButton<span style="color:#4f4f4f !important">-></span>setToolTip(QStringLiteral(<span style="color:#009900 !important">"搜索"</span>));
pSearchButton<span style="color:#4f4f4f !important">-></span>setStyleSheet(<span style="color:#009900 !important">"QPushButton{border-image:url(:/images/icon_search_normal); background:transparent;} \QPushButton:hover{border-image:url(:/images/icon_search_hover)} \QPushButton:pressed{border-image:url(:/images/icon_search_press)}"</span>);<span style="color:#880000 !important">//防止文本框输入内容位于按钮之下</span>
QMargins margins <span style="color:#4f4f4f !important">=</span> m_pSearchLineEdit<span style="color:#4f4f4f !important">-></span>textMargins();
m_pSearchLineEdit<span style="color:#4f4f4f !important">-></span>setTextMargins(margins<span style="color:#4f4f4f !important">.</span>left(), margins<span style="color:#4f4f4f !important">.</span>top(), pSearchButton<span style="color:#4f4f4f !important">-></span>width(), margins<span style="color:#4f4f4f !important">.</span>bottom());
m_pSearchLineEdit<span style="color:#4f4f4f !important">-></span>setPlaceholderText(QStringLiteral(<span style="color:#009900 !important">"请输入搜索内容"</span>));QHBoxLayout <span style="color:#4f4f4f !important">*</span>pSearchLayout <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QHBoxLayout();
pSearchLayout<span style="color:#4f4f4f !important">-></span>addStretch();
pSearchLayout<span style="color:#4f4f4f !important">-></span>addWidget(pSearchButton);
pSearchLayout<span style="color:#4f4f4f !important">-></span>setSpacing(<span style="color:#006666 !important">0</span>);
pSearchLayout<span style="color:#4f4f4f !important">-></span>setContentsMargins(<span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">0</span>);
m_pSearchLineEdit<span style="color:#4f4f4f !important">-></span>setLayout(pSearchLayout);connect(pSearchButton, SIGNAL(clicked(bool)), this, SLOT(search()));</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
槽函数实现
<span style="color:#000000"><code class="language-Qt"><span style="color:#006666 !important">void</span> Widget<span style="color:#006666 !important">::search</span>()
{QString strText <span style="color:#4f4f4f !important">=</span> m_pSearchLineEdit<span style="color:#4f4f4f !important">-></span>text();<span style="color:#000088 !important">if</span> (<span style="color:#4f4f4f !important">!</span>strText<span style="color:#4f4f4f !important">.</span>isEmpty()){QMessageBox<span style="color:#006666 !important">::information</span>(this, QStringLiteral(<span style="color:#009900 !important">"搜索"</span>), QStringLiteral(<span style="color:#009900 !important">"搜索内容为%1"</span>)<span style="color:#4f4f4f !important">.</span>arg(strText));}
}</code></span>
Qt 之自定义搜索框QLineEdit相关推荐
- Qt 之自定义搜索框
简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...
- Qt学习之自定义搜索框(两种方案)
目录 一.前言 二.实现效果 三.方法一 1.初始化按钮 2.初始化编辑框 3.按钮添加至编辑框上方 四.方法二 1.初始化 QAction 2.初始化编辑框并添加 QAction 五.全部代码 1. ...
- Qt 之 自定义提示信息框—迅雷风格
一.简述 最近一直在研究迅雷9的界面,花了点时间做了几个通用的提示信息框,整体风格与迅雷9界面相同.支持模态和非模态两种模式窗口.提示框效果见下图. 我们可以根据设置不同的参数来设置提示框标题.显示内 ...
- android 自定义搜索框edittext,Android编程自定义搜索框实现方法【附demo源码下载】...
本文实例讲述了Android编程自定义搜索框实现方法.分享给大家供大家参考,具体如下: 先来看效果图吧~ 分析:这只是模拟了一个静态数据的删除与显示 用EditText+PopupWindow+lis ...
- iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)
很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...
- vue实现搜索框记录搜索历史_云开发版的微信商城小程序第四章,首页自定义搜索框的实现...
四,首页自定义搜索框的实现 我们的搜索框,会在多个页面使用,所以我们这里把搜索框做成一个自定义组件,这样就可以在别的地方很方便的使用我们的搜索框了. 老规矩,先看效果图 4-1,自定义搜索框组件 我们 ...
- 小程序自定义搜索框_将自定义搜索提供程序添加到Windows 7以及高级搜索技巧
小程序自定义搜索框 Disclaimer: Many of my tips have crashed cars, broken lights, and caused lawnmowers to go ...
- 前端开发:自定义搜索框(含联想搜索)
在前端开发过程中,搜索功能是比较常见的功能,也是在项目开发过程中比较常见的需求.搜索框功能常用的几种需求也都是大同小异,但是实用的搜索框可以作为一个例子来写是很有必要的,那么本篇博文就来分享一下前端开 ...
- 微信小程序自定义搜索框(searchbar)
微信小程序自定义搜索框(searchbar) 样式截图展示: 功能描述:微信小程序页面需要加入搜索框时,可以直接引用到页面中使用,当用户未输入任何关键字时如第一张图所示,当用户输入要搜索的关键字时如第 ...
最新文章
- Adam那么棒,为什么还对SGD念念不忘 (1) —— 一个框架看懂优化算法
- 原来JScript中的关键字'var'还是有文章的
- 如何安装fedora13的显卡驱动
- vue从入门到精通之进阶篇(二)组件通信:兄弟组件通信
- 如何更sql sever数据库表的所有者
- LeetCode 23. 合并K个排序链表(优先队列)
- c语言编程算法精选,c语言经典程序算法【DOC精选】.doc
- 详述Redo日志中操作系统块头信息损坏的处理过程
- mysql正则mybatis中用法_SQL 正则表达式及mybatis中使用正则表达式
- JUC种常用的辅助类
- hp打印机没连上计算机,有关如何将HP打印机连接到计算机,如何添加打印机的详细步骤-...
- 电子电路笔记----基本放大电路--半导体三极管
- 今天才知道!华为手机摄像头可不止能拍照,它还能提高办公效率
- 【本地调试环境一键安装包】php+mysql平台搭建集成软件总汇【php环境汇总】
- CogColorExtractorTool工具功能
- JS实现动态添加和删除div
- Android apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏。
- z3735f android x86,随大流,也装了个z3735f的小主机(1223更新功耗图)
- 就地初始化与列表初始化
- SEO工具MIPCMS搜索引擎提交器 v1.1