[Qt] searchBox 搜索框实现
[Qt] searchBox 搜索框实现
也就是在lineEdit中加入button。在搜索框的右边会有个小小的搜索图标,输入内容之后,搜索的图标会变成叉叉。
类中的IconHelper见我的另一篇博文:http://www.cnblogs.com/mdgsf/p/4841272.html
#ifndef CSEARCHBOX_H
#define CSEARCHBOX_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class CSearchBox : public QWidget
{
Q_OBJECT
public:
explicit CSearchBox(QWidget *parent = 0);
~CSearchBox();
signals:
void sigSearch(QString str);
void sigClear();
protected:
bool eventFilter(QObject *obj, QEvent *event);
public slots:
void slot_text_Edited(QString str);
void slot_btn_clicked();
private:
QLineEdit *m_pLineEdit;
QPushButton *m_pBtn;
enum EBtnStatus{ESEARCH, ECLOSE};
EBtnStatus m_iCurBtnStatus;
};
#endif // CSEARCHBOX_H
#include "searchbox.h"
#include <QHBoxLayout>
#include <QKeyEvent>
#include "iconhelper.h"
CSearchBox::CSearchBox(QWidget *parent)
: QWidget(parent)
{
m_pLineEdit = new QLineEdit(this);
m_pBtn = new QPushButton(m_pLineEdit);
QSize size = QSize(20, m_pLineEdit->sizeHint().height());
m_pBtn->setMinimumSize(size);
m_pBtn->setMaximumSize(size);
m_pBtn->setFocusPolicy(Qt::NoFocus);
m_pBtn->setFlat(true);
m_pBtn->setCursor(QCursor(Qt::PointingHandCursor));
m_pBtn->setText(tr("Search"));
m_iCurBtnStatus = ESEARCH;
IconHelper::Instance()->SetIcon(m_pBtn, 0xf002);
//0xf002 is search btn icon
//0xf00d is close btn icon
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins(0, 0, 0, 0);
buttonLayout->addStretch();
buttonLayout->addWidget(m_pBtn);
m_pLineEdit->setLayout(buttonLayout);
m_pLineEdit->setTextMargins(0, 1, size.width(), 1);
m_pLineEdit->installEventFilter(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(m_pLineEdit);
this->setLayout(mainLayout);
QString qss = QString("QPushButton {background: gray; color: white; border: 1 solid gray;min-width: 20px;}")
+ QString("QPushButton:hover {background: black; color: white; border: 1 solid black;}")
+ QString("QPushButton:pressed {background: white;color: black;}");
this->setStyleSheet(qss);
connect(m_pLineEdit, SIGNAL(textEdited(QString)),
this, SLOT(slot_text_Edited(QString)) );
connect(m_pBtn, SIGNAL(clicked()),
this, SLOT(slot_btn_clicked()));
}
CSearchBox::~CSearchBox()
{
}
void
CSearchBox::slot_text_Edited(QString str)
{
int iTextLen = str.size();
if(iTextLen == 0)
{
m_iCurBtnStatus = ESEARCH;
IconHelper::Instance()->SetIcon(m_pBtn, 0xf002);
emit sigClear();
}
else
{
m_iCurBtnStatus = ECLOSE;
IconHelper::Instance()->SetIcon(m_pBtn, 0xf00d);
}
}
void
CSearchBox::slot_btn_clicked()
{
if(m_iCurBtnStatus == ESEARCH)
{
//This is impossible
}
else
{
m_pLineEdit->clear();
m_pLineEdit->setFocus();
m_iCurBtnStatus = ESEARCH;
IconHelper::Instance()->SetIcon(m_pBtn, 0xf002);
emit sigClear();
}
}
bool
CSearchBox::eventFilter(QObject *obj, QEvent *event)
{
if(m_pLineEdit == qobject_cast<QLineEdit*>(obj))
{
QString str = m_pLineEdit->text().trimmed();
if( (str.size() > 0) &&
(event->type() == QEvent::KeyPress))
{
QKeyEvent *keyEvent = (QKeyEvent*)(event);
if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
{
m_pLineEdit->setFocus();
emit sigSearch(str);
return true;
}
}
}
return QObject::eventFilter(obj, event);
}
//调用方式
CSearchBox *pSearchBtn = new CSearchBox(ui->widget);
connect(pSearchBtn, SIGNAL(sigSearch(QString)),
this, SLOT(slot_getSearchInfo(QString)) );
void
Widget::slot_getSearchInfo(QString str)
{
QMessageBox::information(this, "test", str);
}
[Qt] searchBox 搜索框实现的更多相关文章
- 第二百一十节,jQuery EasyUI,SearchBox(搜索框)组件
jQuery EasyUI,SearchBox(搜索框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 SearchBox(搜索框)组件的使用方法,这个组 ...
- SearchBox( 搜索框) 组件
一. 加载方式//class 加载方式<input id="ss" class="easyui-searchbox" style="width: ...
- EasyUI - SearchBox 搜索框
效果: html代码: <input id="ss"/> <div id="mm"> <div data-options=&quo ...
- Qt之自定义搜索框
简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...
- 【Qt】Qt之自定义搜索框【转】
简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 简述 效果 细节分析 Coding 源码下载 效果 ...
- EasyUI基础searchbox&progressbar(搜索框,进度条)
easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...
- qt自己定义搜索框(超简单,带效果图)
1. 什么也不要说.先上效果图: 2. 代码 头文件: #ifndef APPSEARCHLINE_H #define APPSEARCHLINE_H #include <QLineEdit&g ...
- Qt之自定义搜索框——QLineEdit里增加一个Layout,还不影响正常输入文字(好像是一种比较通吃的方法)
简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...
- Qt 搜索框
一.前言 用户需要输入文本时,可使用QLineEdit控件进行编辑输入,缺点是样式相对单一. 在使用百度搜索输入框时,发觉比较人性化,故采用QLineEdt+QPushButton通过css样式实现自 ...
随机推荐
- CAsyncSocket
本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 它是一个异步非阻塞Socket封装类,CAsyncSocket::Create()有一个参数指明了你想要处理哪些Socket事 ...
- 判断Http请求由手机端发起,还是有电脑端发起
某些情形,我们需要判断Http请求是来自手机端还是电脑端,关键是取得User-Agent的信息,进行筛选判断即可. 核心类如下: public static boolean isMobileDevic ...
- 常用元素的属性/方法 attr / val / html /text
常用元素的属性/方法 得到一个元素的高度, $("#myid").height() 得到一个元素的位置, $("#myid").offset() 返回的是一个o ...
- @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
1.带有Render的方法返回值是void,在方法内部进行输出: 不带的返回值类型为MvcHtmlString,所以只能这样使用: @Html.Partial 对应 @{Html.RenderPart ...
- const的重载
class A { private: int a; public: A(int x) :a(x){}//构造函数并不能重载 void display(){ cout << "no ...
- uva 498 - Polly the Polynomial
UVa 498: Polly the Polynomial | MathBlog #include <cstdio> #include <cstdlib> using name ...
- js和jquery中的触发事件
改别人的坑,遇到jquery选择器和fireEvent混用,不认识fireEvent方法报错. js的方法不能使用jquery的选择器去调用. 1.fireEvent (IE上的js方法 ) 我们来看 ...
- Css3抖动
http://files.cnblogs.com/xinlinux/csshake.min.css <div class="shake">AAA</div> ...
- vmware安装Linux时无法打开xpdf
vmware10+redhat9 在装第二张镜像文件时,出现如下提示:无法打开xpdf-2.01-8软件包...... 解决方法: vmware中,虚拟机->设置->硬件->CD/D ...
- Lua 学习笔记(一)
Lua学习笔记 1.lua的优势 a.可扩张性 b.简单 c.高效率 d.和平台无关 2.注释 a.单行注释 -- b.多行注释 --[[ --]] 3.类型和 ...