当前位置: 首页 > news >正文

QT样式表实现一键换肤

思路:QT样式表统一卸载一个txt文件中。启动程序时加载这个文件。然后使用qApp设置整体的样式表。即可实现程序的样式设置。如果需要换肤,可远程更新这个txt文件。然后重启程序,重新加载新的样式表。

加载的样式表如下

 QString qss;
    QFile file(":/qss/backGround.css");
    if(file.open(QFile::ReadOnly)){
        qss = QLatin1String(file.readAll());
        qApp->setStyleSheet(qss);
    }else{
        qDebug() <<"11111111";
    }
    file.close();

 效果如下面所示:

 样式表文件内容

QPushButton {
    /* 基础样式 */
    background-color: #4CAF50;  /* 绿色背景 */
    border: 1px solid #45a049;  /* 深绿色边框 */
    border-radius: 4px;        /* 圆角 */
    color: white;              /* 白色文字 */
    padding: 5px 15px;         /* 内边距 */
    font: bold 12px "Arial";   /* 字体设置 */

    /* 渐变效果 */
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                stop:0 #5CBF5C, stop:1 #4CAF50);
}

/* 鼠标悬停效果 */
QPushButton:hover {
    background-color: #45a049;
    border: 1px solid #409040;
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                stop:0 #4CAF50, stop:1 #45a049);
}

/* 按下效果 */
QPushButton:pressed {
    background-color: #3d8b40;
    border: 1px solid #367c39;
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                stop:0 #45a049, stop:1 #3d8b40);
    padding: 6px 15px 4px 15px;  /* 模拟按下效果 */
}

/* 禁用状态 */
QPushButton:disabled {
    background-color: #cccccc;
    border: 1px solid #bbbbbb;
    color: #666666;
}

QLineEdit {
    /* 基础样式 */
    border: 2px solid #cccccc;      /* 默认边框 */
    border-radius: 5px;            /* 圆角半径 */
    padding: 8px 12px;             /* 内边距 */
    font: 12px "Arial";            /* 字体设置 */
    background-color: #ffffff;     /* 背景色 */
    color: #333333;                /* 文字颜色 */
    selection-background-color: #4CAF50;  /* 选中文本背景色 */

    /* 阴影效果 */
    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1) inset; /* 内阴影 */
}

/* 焦点状态 */
QLineEdit:focus {
    border: 2px solid #2196F3;     /* 聚焦时蓝色边框 */
    background-color: #f8fdff;     /* 浅蓝色背景 */
    box-shadow: 0 0 5px rgba(33, 150, 243, 0.2); /* 外发光效果 */
}

/* 悬停效果 */
QLineEdit:hover {
    border: 2px solid #a0a0a0;     /* 悬停时灰色加深 */
}

/* 禁用状态 */
QLineEdit:disabled {
    background-color: #f5f5f5;     /* 浅灰背景 */
    color: #999999;               /* 灰色文字 */
    border: 2px solid #e0e0e0;    /* 浅灰边框 */
}

/* 输入验证失败状态 */
QLineEdit[invalid="true"] {
    border: 2px solid #ff4444;    /* 红色边框 */
    background-color: #fff5f5;    /* 浅红背景 */
}

/* 输入验证成功状态 */
QLineEdit[valid="true"] {
    border: 2px solid #4CAF50;    /* 绿色边框 */
    background-color: #f5fff5;    /* 浅绿背景 */
}

/* 占位符文字样式 */
QLineEdit::placeholder {
    color: #aaaaaa;              /* 灰色提示文字 */
    font-style: italic;         /* 斜体样式 */
}

/* 现代扁平化风格按钮 */
QWidget[flag="title"] QPushButton {
    /* 基础样式 */
    background-color: #2196F3;        /* 主色背景 */
    color: white;                    /* 文字颜色 */
    border: none;                    /* 无边框 */
    border-radius: 25px;             /* 圆形按钮 */
    padding: 12px 24px;              /* 舒适内边距 */
    font: bold 14px "Segoe UI";      /* 现代字体 */
    min-width: 100px;                /* 最小宽度 */
    min-height: 40px;                /* 最小高度 */

    /* 动态阴影 */
    box-shadow: 0 4px 6px rgba(33, 150, 243, 0.2);
    transition: all 0.3s ease;       /* 平滑过渡动画 */
}

/* 悬停效果 - 浮动提升 */
QWidget[flag="title"] QPushButton:hover {
    background-color: #42A5F5;       /* 浅色主调 */
    box-shadow: 0 6px 12px rgba(33, 150, 243, 0.3);
    transform: translateY(-2px);     /* 上浮效果 */
}

/* 按下效果 - 点击反馈 */
QWidget[flag="title"] QPushButton:pressed {
    background-color: #1E88E5;       /* 深色主调 */
    box-shadow: 0 2px 4px rgba(33, 150, 243, 0.2);
    transform: translateY(1px);      /* 下沉效果 */
}

/* 禁用状态 - 灰度显示 */
QWidget[flag="title"] QPushButton:disabled {
    background-color: #BBDEFB;       /* 浅灰主色 */
    color: #757575;                 /* 灰色文字 */
    box-shadow: none;
}

/* 特殊样式变体 */
QWidget[flag="title"] QPushButton.warning {
    background-color: #FF5252;       /* 警告红色 */
    box-shadow: 0 4px 6px rgba(255, 82, 82, 0.2);
}
QWidget[flag="title"] QPushButton.warning:hover {
    background-color: #FF867C;
    box-shadow: 0 6px 12px rgba(255, 82, 82, 0.3);
}

QWidget[flag="title"] QPushButton.success {
    background-color: #4CAF50;       /* 成功绿色 */
    box-shadow: 0 4px 6px rgba(76, 175, 80, 0.2);
}
QWidget[flag="title"] QPushButton.success:hover {
    background-color: #66BB6A;
    box-shadow: 0 6px 12px rgba(76, 175, 80, 0.3);
}

/* 图标按钮 */
QWidget[flag="title"] QPushButton.icon-button {
    padding: 8px;
    min-width: 40px;
    min-height: 40px;
}


QWidget[flag="title"] QToolTip{
    background-color: #FFF3CD;
    color: #856404;
    border: 1px solid #FFE594;
    border-radius: 3px;
    padding: 5px;
    font: 12px 'Segoe UI';
}



上面这段样式表参考这张图设置

1、程序中所有该类型控件均会采用此样式表设置 
2、控件类型前使用.表明继承自这个控件的子类自定义控件不会覆盖该样式表设置
3、根据控件名匹配样式表的具体设置
4、在程序中通过元对象系统设置属性选择器,匹配对应的样式表。(这个很方便)

属性选择器在程序中的代码体现

 ui->TitleWidget->setProperty("flag","title");
 ui->midWidget->setProperty("flag","title");

相关文章:

  • Windows下 Eigen3 安装
  • 定制一款国密浏览器(4):修改浏览器logo
  • C++23 多维下标运算符:探索 P2128R6 提案
  • AI领域再突破,永洪科技荣获“2025人工智能+创新案例”奖
  • SpringBoot集成阿里云文档格式转换实现pdf转换word,excel
  • GO语言入门-反射5(结构体的Tag)
  • 第二章 Python爬虫篇—数据解析与提取
  • Python学习笔记(三)
  • Active Directory 域服务
  • <C#>在 .NET 开发中,依赖注入, 注册一个接口的多个实现
  • react从零开始的基础课
  • 【VSCode配置】运行springboot项目和vue项目
  • 精准测试背后的关键技术:电机试验平台(北重数控滑台设计专业)
  • STM32 模块化开发指南 · 第 2 篇 如何编写高复用的外设驱动模块(以 UART 为例)
  • 程序化广告行业(77/89):融资、并购与上市全景洞察
  • UE的AI判断队伍归属的机制:IGenericTeamAgentInterface接口
  • 基础数学:线性代数与优化理论
  • 机器学习 从入门到精通 day_04
  • 分治-归并系列一>翻转对
  • 移动端六大语言速记:第14部分 - 数据库操作
  • 做汽车网站/免费推广方式都有哪些
  • 做网站和淘宝美工 最低电脑/企业seo优化
  • 海南省建设网站的公司/微信附近人推广引流
  • 济南网站制作多少钱/怎么给产品找关键词
  • wordpress调整语言/seo云优化软件破解版
  • 网站建设与管理单招/肇庆seo外包公司