随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Qt创建一个自定义QPushButton

一、概述

  使用Qt创建一个通用的QPushButton。应用一些样式把按钮做的好看一些。

  步骤:

    1.新建一个Button类然后继承QPushButton

    2.设置Button的通用样式(ps:使用.qss文件的形式应用样式)

    3.个性化设置不同的样式

    4.做一个圆角按钮、带图标按钮、带背景图片按钮

二、示例

  1.创建Button.h和Button.cpp继承QPushButton 【默认样式】

复制代码
#include "Button.h"
#include <QDebug>
#include <QIcon>

Button::Button(QWidget* parent)
    : QPushButton(parent)
{
    //this->resize(80, 36);
    this->setFixedHeight(36);
  //设置默认的样式 QString css
= StyleHelper::getStyleSheet(StyleConfig::COMMON_BUTTON_STYLE()); this->setStyleSheet(css); } /** * 设置自定义按钮样式 * 样式直接从StyleConfig中获取,里面存放了一些通用样式 */ void Button::setCustomStyleSheet(QString qss) { QString css = StyleHelper::getStyleSheet(qss); this->setStyleSheet(css); } Button::~Button() { }
复制代码

  2.个性化设置按钮的样式 【第二个按钮样式】【第三个按钮样式】

复制代码
Button *btn = new Button(this);
btn->move(0, 5);
btn->setText("点击按钮应用样式");

//给按钮加上图标
Button* btn2 = new Button(this);
btn2->setCustomStyleSheet(StyleConfig::COMMON_BUTTON_SHAPE_BLACK_R10());
btn2->setText("第二个按钮");
btn2->setIcon(QIcon("images/ai_suanfa.png"));//给按钮设置图标
btn2->move(0, btn->y()+btn->height()+5);

//给按钮加上背景图片
Button* btn3 = new Button(this);
btn3->resize(100, 36);
btn3->setCustomStyleSheet(StyleConfig::COMMON_BUTTON_IMAGE_BG());
btn3->move(5,btn2->y()+ btn2->height()+5);
复制代码

  3.三种按钮的演示效果

 

posted on   飘杨......  阅读(508)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2022-11-28 SDP字段解释一览表
2013-11-28 eclipse中查看安装的所有插件,并选择性的将其卸载
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示