(转载)Qt:给QLineEdit加上一个搜索按钮

摘要:
(重印)http://www.cppblog.com/biao/archive/2011/10/27/159209.html渲染如下:项目文件:**************************************
(转载)http://www.cppblog.com/biao/archive/2011/10/27/159209.html

 

效果图如下:

(转载)Qt:给QLineEdit加上一个搜索按钮第1张

 

(转载)Qt:给QLineEdit加上一个搜索按钮第2张

 

工程文件:/Files/biao/SearchButton.7z

 

/**********************************************

 *                 SearchButton.h

 *********************************************/

 

#ifndef SEARCHBUTTON_H

#define SEARCHBUTTON_H

 

#include <QPushButton>

 

class QLineEdit;

class QString;

 

class SearchButton : public QPushButton {

    Q_OBJECT

public:

    SearchButton(const QString &text, QLineEdit *edit);

};

 

#endif // SEARCHBUTTON_H

 
 

/**********************************************

 *                  SearchButton.cpp

 *********************************************/

#include "SearchButton.h"

#include <QtGui/QLineEdit>

#include <QtGui/QHBoxLayout>

 

SearchButton::SearchButton(const QString &text, QLineEdit *edit)

    : QPushButton(text, edit) {

    QSize size = QSize(40, edit->sizeHint().height());

    setMinimumSize(size);

    setMaximumSize(size);           // 设置按钮的大小为图片的大小

    setFocusPolicy(Qt::NoFocus);    // 得到焦点时,不显示虚线框

    setFlat(true);

    setText(text);

    setCursor(QCursor(Qt::PointingHandCursor));

 

    QHBoxLayout *buttonLayout = new QHBoxLayout();

    buttonLayout->setContentsMargins(0, 0, 0, 0);

    buttonLayout->addStretch();

    buttonLayout->addWidget(this);

    edit->setLayout(buttonLayout);

 

    // 设置输入框中文件输入区,不让输入的文字在被隐藏在按钮下

    edit->setTextMargins(0, 1, size.width(), 1);

 

    // 设置style sheet

    /*.SearchButton {

        background: gray; color: white; border: 1 solid gray;

        min- 40px;

    }

 

    .SearchButton:hover {

        background: black; color: white; border: 1 solid black;

    }

 

    .SearchButton:pressed {

        background: white;

        color: black;

    }*/

 

    // 为了方便起见, 帮把 style sheet 写到代码里, 实际工作中应该放到专用的style sheet, 方便修改

    QString qss = QString(".SearchButton {background: gray; color: white; border: 1 solid gray;min- 40px;}")

            + QString(".SearchButton:hover {background: black; color: white; border: 1 solid black;}")

            + QString(".SearchButton:pressed {background: white;color: black;}");

    setStyleSheet(qss);

}

 

/**********************************************

 *                  Widget.cpp

 *********************************************/

#include "Widget.h"

#include "ui_Widget.h"

#include "SearchButton.h"

 

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {

    ui->setupUi(this);

    new SearchButton(tr("搜索"), ui->lineEdit_1); // 使用方法

    new SearchButton(tr("搜索"), ui->lineEdit_2);

}

 

Widget::~Widget() {

    delete ui;

}

免责声明:文章转载自《(转载)Qt:给QLineEdit加上一个搜索按钮》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Echarts的赋值,设置数据去掉字符串中的某几位下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CSS美化自己的完美网页

CSS美化自己的完美网页 CSS概述 css样式: css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化,CSS的可以使页面更加的美观。基本上所有的html页面都或多或少的使用css。 CSS 指层叠样式表 (CascadingStyleSheets) 样式定义如何显示HTML 元素 样式通常存储在样式表...

WPF 自定义滚动条样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Re...

CSS之纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等)

图形包括基本的矩形、圆形、椭圆、三角形、多边形,也包括稍微复杂一点的爱心、钻石、阴阳八卦等。当然有一些需要用到CSS3的属性,所以在你打开这篇文章的时候,我希望你用的是firefox或者chrome,当然IE也能看一部分的。那好,下面就一起来看看我们是如何用纯CSS来画这些图形的,如果你也觉得很震撼,推荐给你的朋友吧。 1、正方形 最终效果: CSS...

CSS 基础知识(认识选择器)

定义: 层叠样式表(CascadingStyleSheets) 主要是用于定义HTML内容在浏览器内的显示样式,如文字大小、颜色、字体加粗等。 好处是通过定义某个样式,可以让不同网页位置的文字有着统一的字体、字号或者颜色等,设置文本和背景属性的能力,为任何元素创建边框及距离 语言特点: 易于修改、使用,将样式定义在HTML元素的style属性中,...

jQuery的过滤器总结

1、内容过滤器 $(function () { // $("a:contains('标签')").css("color","green") // $("div:empty").css() //不能包含文本和标签 $("div:hidden").length //...

CSS3基础(2)—— 文字与字体相关样式、盒子类型、背景与边框相关样式、变形处理、动画功能

一、 CSS3 文字与字体相关样式 1、 给文字添加阴影    text-shadow: length length length ccolor;     属性适用于文本阴影,指定了水平阴影,垂直阴影,模糊的距离,以及阴影的颜色 2、 使用服务器端字体   文本换行:    word-break:norma | keep-all | bread-all...