[Qt] searchBox 搜索框实现

也就是在lineEdit中加入button。在搜索框的右边会有个小小的搜索图标,输入内容之后,搜索的图标会变成叉叉。

类中的IconHelper见我的另一篇博文:http://www.cnblogs.com/mdgsf/p/4841272.html

//searchbox.h

#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

//searchbox.cpp

#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 搜索框实现的更多相关文章

  1. 第二百一十节,jQuery EasyUI,SearchBox(搜索框)组件

    jQuery EasyUI,SearchBox(搜索框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 SearchBox(搜索框)组件的使用方法,这个组 ...

  2. SearchBox( 搜索框) 组件

    一. 加载方式//class 加载方式<input id="ss" class="easyui-searchbox" style="width: ...

  3. EasyUI - SearchBox 搜索框

    效果: html代码: <input id="ss"/> <div id="mm"> <div data-options=&quo ...

  4. Qt之自定义搜索框

    简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...

  5. 【Qt】Qt之自定义搜索框【转】

    简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 简述 效果 细节分析 Coding 源码下载 效果 ...

  6. EasyUI基础searchbox&amp;progressbar(搜索框,进度条)

    easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...

  7. qt自己定义搜索框(超简单,带效果图)

    1. 什么也不要说.先上效果图: 2. 代码 头文件: #ifndef APPSEARCHLINE_H #define APPSEARCHLINE_H #include <QLineEdit&g ...

  8. Qt之自定义搜索框——QLineEdit里增加一个Layout,还不影响正常输入文字(好像是一种比较通吃的方法)

    简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...

  9. Qt 搜索框

    一.前言 用户需要输入文本时,可使用QLineEdit控件进行编辑输入,缺点是样式相对单一. 在使用百度搜索输入框时,发觉比较人性化,故采用QLineEdt+QPushButton通过css样式实现自 ...

随机推荐

  1. 华为OJ:2041 放苹果

    这道题难点不在于代码怎么写,而是思路怎么想. 感觉一般这样的题要么你理好一个思路要么你最后总结出一个公式,要么你自己模拟它的运作方式,用迭代,或者递归的方式来做. 有点像我们曾经学的排列组合. 对于m ...

  2. Nginx 主配置文件参数详解

    Nginx 主配置文件参数详解 Nginx 安装完毕后,会有响应的安装目录,安装目录里 nginx.conf 为 nginx 的主配置文件, ginx 主配置文件分为 4 部分,main(全局配置). ...

  3. LSI SAS 3008配置操作

    配置 LSI SAS 3008 介绍LSISAS3008的配置操作. 4.1 登录CU界面 介绍登录LSISAS3008的CU配置界面的方法. 4.2 创建RAID 介绍在LSISAS3008扣卡上创 ...

  4. 【iOS之runtime、runloop】

    什么是runtime runtime就是运行时,是系统在运行时的一些动态机制,它是一套底层的API,我们平时编写的OC代码,最终会转换为runtime实现. runtime的作用 可以利用runtim ...

  5. vb的LINQ实现

    vb实现LINQ非常简单的例子: Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} Dim allNumbers = From number In nu ...

  6. C# 无边框窗体移动代码

    C# 无边框窗体移动代码 Point _frmPoint = new Point(); //移动前窗体左上角坐标 Point _mousePoint = new Point(); //按下鼠标时坐标 ...

  7. JS 无提示关闭当前窗口

    function teseClose() { window.opener = null; window.open('','_self'); window.close(); }

  8. IPointCollection,ISegmentCollection和IGeometryCollection

    Engine 提供了三个主要的几何图形集合接口用于对几何对象的操作,分别是 IPointCollection,ISegmentCollection 和 IGeometryCollection,这些接口 ...

  9. "sfc/scannow" 修复系统,提示 "windows资源保护无法启动修复服务"(win7)

    原因: ArcGIS9.3安装后对注册空间进行了限制. 解决方案: 1,输入 regeidt 打开注册表. 2,找到 HKEY_LOCAL_MACHINE\System\CurrentControlS ...

  10. Eclipse远程提交hadoop集群任务

    文章概览: 1.前言 2.Eclipse查看远程hadoop集群文件 3.Eclipse提交远程hadoop集群任务 4.小结   1 前言 Hadoop高可用品台搭建完备后,参见<Hadoop ...