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

QT全局所有QSS样式实时切换

方法如下:

void loadQss(int qssType)
{QString name;if (qssType == 1)name = ":/qss/day.qss";elsename = ":/qss/night.qss";QFile file(name);file.open(QFile::ReadOnly);QString qss;qss = file.readAll();qApp->setStyleSheet(qss);file.close();
}

为什么qApp->setStyleSheet(qss);可以做到全局所有样式实时切换,我来看下setStyleSheet的源码。

void QApplication::setStyleSheet(const QString& styleSheet)
{//将样式内容保存到内存,以备候用QApplicationPrivate::styleSheet = styleSheet;//获取当前应用程序的样式QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle*>(QApplicationPrivate::app_style);//第一个判断条件:如果传入样式为空,则保持当前应该用程序样式if (styleSheet.isEmpty()) { // application style sheet removedif (!proxy)return; // there was no stylesheet beforesetStyle(proxy->base);//如果应用程序已经设置了样式,则只是刷新样式,而不会使用函数传入的样式。//这也就告诉我们,为qApp设置样式前,需要卸载当前样式,之后安装样式才能生效} else if (proxy) { // style sheet update, just repolishproxy->repolish(qApp);//当前qApp没有样式,设置我们传进来的样式} else { // stylesheet set the first timeQStyleSheetStyle *newProxy = new QStyleSheetStyle(QApplicationPrivate::app_style);QApplicationPrivate::app_style->setParent(newProxy);setStyle(newProxy);}
}
void QApplication::setStyle(QStyle *style)
{if (!style || style == QApplicationPrivate::app_style)return;//拿到所有控件,设置它们的样式。		QWidgetList all = allWidgets();// clean up the old styleif (QApplicationPrivate::app_style) {if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) {QWidget *w = *it;if (!(w->windowType() == Qt::Desktop) &&        // except desktopw->testAttribute(Qt::WA_WState_Polished)) { // has been polished//清除所有老样式QApplicationPrivate::app_style->unpolish(w);}}}QApplicationPrivate::app_style->unpolish(qApp);}QStyle *old = QApplicationPrivate::app_style; // saveQApplicationPrivate::overrides_native_style =nativeStyleClassName() == QByteArray(style->metaObject()->className());#ifndef QT_NO_STYLE_STYLESHEETif (!QApplicationPrivate::styleSheet.isEmpty() && !qobject_cast<QStyleSheetStyle *>(style)) {// we have a stylesheet already and a new style is being setQStyleSheetStyle *newProxy = new QStyleSheetStyle(style);style->setParent(newProxy);QApplicationPrivate::app_style = newProxy;} else
#endif // QT_NO_STYLE_STYLESHEETQApplicationPrivate::app_style = style;QApplicationPrivate::app_style->setParent(qApp); // take ownership// take care of possible palette requirements of certain gui// styles. Do it before polishing the application since the style// might call QApplication::setPalette() itselfif (QApplicationPrivate::set_pal) {QApplication::setPalette(*QApplicationPrivate::set_pal);} else if (QApplicationPrivate::sys_pal) {clearSystemPalette();initSystemPalette();QApplicationPrivate::initializeWidgetPaletteHash();QApplicationPrivate::initializeWidgetFontHash();QApplicationPrivate::setPalette_helper(*QApplicationPrivate::sys_pal, /*className=*/0, /*clearWidgetPaletteHash=*/false);} else if (!QApplicationPrivate::sys_pal) {// Initialize the sys_pal if it hasn't happened yet...QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette());}// initialize the application with the new style//加载样式QApplicationPrivate::app_style->polish(qApp);// re-polish existing widgets if necessaryif (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) {QWidget *w = *it;if (w->windowType() != Qt::Desktop && w->testAttribute(Qt::WA_WState_Polished)) {if (w->style() == QApplicationPrivate::app_style)QApplicationPrivate::app_style->polish(w);                // repolish
#ifndef QT_NO_STYLE_STYLESHEETelsew->setStyleSheet(w->styleSheet()); // touch
#endif}}for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) {QWidget *w = *it;if (w->windowType() != Qt::Desktop && !w->testAttribute(Qt::WA_SetStyle)) {QEvent e(QEvent::StyleChange);//将新样式应用到所有子控件中QApplication::sendEvent(w, &e);w->update();}}}#ifndef QT_NO_STYLE_STYLESHEETif (QStyleSheetStyle *oldProxy = qobject_cast<QStyleSheetStyle *>(old)) {oldProxy->deref();} else
#endifif (old && old->parent() == qApp) {delete old;}if (QApplicationPrivate::focus_widget) {QFocusEvent in(QEvent::FocusIn, Qt::OtherFocusReason);QApplication::sendEvent(QApplicationPrivate::focus_widget->style(), &in);QApplicationPrivate::focus_widget->update();}
}

通过以上源码我们能看出在qApp的setStyleSheet中会拿到所有控件,清除旧的样式,然后加载新的样式,做到全局所有控件样式的动态切换。

http://www.lryc.cn/news/533005.html

相关文章:

  • MySQL三大版本的演进
  • 利用 IMU 估计人体关节轴向和位置 —— 论文推导
  • 脚本一键生成管理下游k8s集群的kubeconfig
  • 数据库系统概念第六版记录 三
  • YOLOv11-ultralytics-8.3.67部分代码阅读笔记-files.py
  • 微信小程序案例1——制作猫眼电影底部标签导航栏
  • 【大数据技术】搭建完全分布式高可用大数据集群(Kafka)
  • 【服务器知识】如何在linux系统上搭建一个nfs
  • 图片画质增强:轻松提升画质
  • vscode快速接入deepseek 实践操作
  • mapbox进阶,添加绘图扩展插件,绘制圆形
  • Cursor 与多语言开发:全栈开发的利器
  • 2025 CCF BDCI|“基于TPU平台的OCR模型性能优化”一等奖作品
  • FPGA的IP核接口引脚含义-快解
  • 数据库高安全—审计追踪:传统审计统一审计
  • 机器学习 - 需要了解的条件概率、高斯分布、似然函数
  • Spring Boot Web 入门
  • 神经网络|(八)概率论基础知识-二项分布及python仿真
  • 【面试场景】MySQL分布式主键选取
  • 执行git stash drop stash@{x} 时出现error: unknown switch `e‘ 的解决方式
  • 链表和 list
  • windows 蓝牙驱动开发-传输总线驱动程序常见问题
  • Qt修仙之路2-1 炼丹初成
  • 【含开题报告+文档+PPT+源码】基于SpringBoot+Vue宠物预约上门服务预约平台
  • 无线AP之详解(Detailed Explanation of Wireless AP)
  • Spring Boot Actuator与JMX集成实战
  • mac环境下,ollama+deepseek+cherry studio+chatbox本地部署
  • camera光心检测算法
  • 【MySQL】向后兼容设计规范(无回滚场景)
  • 还搞不透stm32单片机启动过程?一篇文章几百字让你彻底看懂!