引言

作为一套GUI框架,Qt是非常强大的。(注:Qt 不仅是一套优秀的GUI框架,同时也是一套出色的应用程序框架)。
在UI的制作方面Qt为广大开发者提供了一套强大而易用的工具,她就是——Qt Style Sheets。
本文将向大家举例介绍如何使用Qt Style Sheets制作个性化的UI界面。例子程序(stylesheetDemo)可通过本文末尾所附链接下载。

UI涉及的东西非常庞杂,Qt Style Sheets也包含许许多多的内容,因此本文并不试图对Qt Style Sheets进行系统的理论性的详解,那需要数十倍于本文的篇幅。本文仅通过几个例子,将大家引入Qt Style Sheets的大门,以后如有更多需求大家直接在Qt Assistant中查询Qt Style Sheets并且结合自己写的程序进行测试就可以了。

测试设备

Nokia N8

预备知识

Style sheets 是由一系列的style rules组成的。一条style rule 由选择器selector和声明declaration这两部分构成。selector说明这条规则在哪些widgets上起作用,declaration说明要在这些widgets上设置什么属性properties。例如:

QPushButton, QLineEdit  { color: red; background-color: white }

在上面这条style rule中QPushButton, QLineEdit 是两个选择器,中间用逗号连接。 { color: red; ">Qt widgets所支持的所有属性列表请查阅List of Properties

Tab1:QLineEdit QGroupBox QRadioButton QCheckBox QLabel(使用qss文件)

例子程序的UI结构非常简单,只有两部分,上方是一个有三个tab页面的QTabWidget,下面是一个QPushButton。
下面我们先来制作TabWidget的第一个页面Tab1。先看一下效果图:
图一:

图二:

这张是没有使用StyleSheet的样子:

Tab1中使用到了五种控件。如果控件较多或比较复杂,我们可以通过使用qss文件来设置Style Sheet。首先我们新建一个文本文档,后缀名改为qss,然后用文本编辑器比如记事本打开它,将我们设置的Style Sheets写进去然后保存就可以了。本例程创建的qss文件叫stylesheetDemo.qss,于是我们在程序中只需要写如下几行代码就可以使我们写在qss文件中的Style Sheets起作用:

QFile file(":/qss/stylesheetDemo.qss");
file.open(QFile::ReadOnly);
QTextStream filetext(&file);
QString stylesheet = filetext.readAll();
ui->tab->setStyleSheet(stylesheet);

程序中stylesheetDemo.qss已加入到资源文件,其中ui->tab就是TabWidget中的第一个tab页面。

下面是stylesheetDemo.qss的内容:

QGroupBox {
background-image: url(:/pics/background.png);
border-radius: 30px;
}
 
QLabel {
color: gray;
}
 
QLineEdit {
background: qradialgradient(cx:0, cy:0, radius: 1,
fx:0.5, fy:0.5, stop:0 white, stop:1 rgba(0,190,0, 60%));
border-radius: 9px;
}
 
 
 
QCheckBox:checked {
color: green;
}
 
QCheckBox::indicator {
width: 25px;
height: 25px;
}
 
QCheckBox::indicator:checked {
image: url(:/pics/checkbox.gif);
}
 
 
 
QRadioButton{
spacing: 10
}
 
QRadioButton::indicator {
width: 25px;
height: 25px;
}
 
QRadioButton:checked {
color: rgb(230,115, 0);
}
 
QRadioButton::indicator:checked {
image: url(:/pics/radioButton.png);
}

其中border-radius指的是边框四角的半径,这一属性可以制作出弧形的边框。

background-image属性设置控件的背景图片。
background-color 设置控件的背景色,我们这里对QLineEdit使用了渐变的颜色,这里利用了Qt提供的qradialgradient
一个冒号说明的是状态,例如“:checked”指的是当此控件被checked的时候。
双冒号说明的是子控件,例如“::indicator”指的是 QCheckBox、QRadioButton、QAbstractItemView 或者是可以被选中的 QMenu item或QGroupBox的indicator。

这里需要注意的是,由于QRadioButton和QCheckBox在Symbian上的实现有一点缺憾,就是他们在获得焦点的时候,其新的背景颜色会完全覆盖掉控件,用户就看不到控件了。因此我们需要去掉他们获得焦点的能力:

ui->checkBox->setFocusPolicy(Qt::NoFocus);
ui->checkBox_2->setFocusPolicy(Qt::NoFocus);
ui->radioButton->setFocusPolicy(Qt::NoFocus);
ui->radioButton_2->setFocusPolicy(Qt::NoFocus);

Tab2:QTextBrowser (在代码中setStyleSheet)

程序中对TextBrowser设置了一种透明的背景颜色,并且是像彩虹一样逐渐变化的颜色。这主要是利用了qlineargradient。下面分别是竖屏和横屏状态下Tab2的效果图:

图三:

图四:

这张是没有使用StyleSheet的样子:

我们这里直接在代码中对textBrowser设置StyleSheet:

    ui->textBrowser->setStyleSheet("\
color: rgb(127, 0, 63);\
color: rgb(0, 0, 153); font-weight: bold;">\
stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), stop: 0.4 rgba(255, 255, 0, 30%), \
stop: 0.6 rgba(0, 255, 0, 30%), stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); \
selection-color: white;\
selection-\
border: 2px groove gray;\
border-radius: 30px;\
padding: 2px 4px;");

Tab3:QWebView

QWebView也是可以通过Qt Style Sheets的方式在一定程度上修改网页呈现在用户面前的样子。
例程中对WebView设置了完全透明的背景色,下面是效果图:
图五:

图六:

图七:

这张是没有使用StyleSheet的样子:

ui->webView->setStyleSheet("border: 1px groove gray; border-radius: 5px;  ");

这里要注意,这样设置只对本身透明的网页是有效的,如果网页自己设置了白色背景,则我们还是看不到透明的效果。

还要额外说明一点,如果不对webView的border属性进行设置,而使用QWebView在N8上的默认实现,则网页中的Button是黑色的背景,Button上的字是看不清的。
要想完全使网页按照我们自定义的样式进行显示(渲染),最根本的解决办法是我们修改Webkit,从而渲染出我们需要的样子。

QPushButton QTabWidget

对比图一和图二,我们会发现exit按钮按下和没有按下时的背景、文字颜色和文字位置都是不一样的,其中背景是通过border-image实现的,文字的位置是通过padding来控制的。

    ui->ExitpushButton->setStyleSheet("\
QPushButton {\
color: white;\
border-image: url(:/pics/button.png);\
border-width: 12px;\
padding: -12px 0px;\
min-height: 25px;\
min-width: 60px;\
}\
QPushButton:pressed {\
color: lightgray;\
border-image: url(:/pics/button-pressed.png); \
padding-top: -10px;\
padding-bottom: -16px;\
}\
");

对于三个tab标签的样式是这样设置的,其中!selected表示没有选中,margin-top: 5px;会使得选中的tab比没选中的高5个像素。

    ui->tabWidget->setStyleSheet("\
QTabBar::tab {\
color: rgb(84,2,119);\
background-image: url(:/pics/wood.jpg); \
border: 2px solid rgb(68,66,64);\
border-bottom-color: rgb(68,66,64); \
border-top-left-radius: 20px;\
border-top-right-radius: 20px;\
max-height: 21px;\
min-width: 8ex;\
padding: 2px;\
} \
QTabWidget::tab-bar {\
alignment: center;\
} \
QTabBar::tab:!selected {\
margin-top: 5px; \
}\
QTabBar::tab:selected {\
color: rgb(255,0,128); \
}\
");

最后横竖屏背景图片的切换也是通过stylesheet实现的:

void MainWindow::resizeEvent ( QResizeEvent * event )
{
enum ScreenMode currentscreenMode;
if(size().height()> size().width())
currentscreenMode = Portrait;
else
currentscreenMode = Landscape;
 
if (currentscreenMode!=scmode)
{
scmode = currentscreenMode;
switch(scmode)
{
case Portrait:
this->setStyleSheet("QMainWindow{ background-image: url(:/pics/bgPortrait.jpg)}");
break;
case Landscape:
this->setStyleSheet("QMainWindow{ background-image: url(:/pics/bgLandscape.jpg)}");
break;
default:
break;
}
}
}

下载样例程序

Media:stylesheetDemo.zip

使用Qt Style Sheets制作UI特效的更多相关文章

  1. Qt Style Sheets制作UI特效

    使用Qt Style Sheets制作UI特效  博客出处:http://developer.nokia.com/community/wiki/%E4%BD%BF%E7%94%A8Qt_Style_S ...

  2. Qt Style Sheets Examples(官方例子目录,很全)

    Contents Style Sheet Usage Customizing the Foreground and Background Colors Customizing Using Dynami ...

  3. Qt Style Sheets Reference

    Qt Style Sheets support various properties, pseudo-states, and subcontrols that make it possible to ...

  4. Qt Style Sheets帮助文档 Overview

    Qt Style Sheets are a powerful mechanism that allows you to customize the appearance of widgets, in ...

  5. Qt4.7文档翻译:Qt样式单参考,Qt Style Sheets Reference(超长,超全)

    内容目录 Qt样式单参考 可进行样式设置的部件列表 属性列表 图标列表 属性类型列表 伪状态列表 子控件列表 Qt样式单参考 Qt样式单支持各种属性.伪状态和子控件,这样使得妳能够自行设计部件的外观. ...

  6. Qt Style Sheets Examples(QT真是有很全的文档)

    http://doc.qt.io/qt-5/stylesheet-examples.html http://doc.qt.io/qt-4.8/stylesheet.html

  7. Qt Style Sheets Examples——定制前景色和背景色

    例子取自:http://qt-project.org/doc/qt-4.8/stylesheet-examples.html 以lineEdit为例 (1)设置某个lineEdit的背景色为黄色 li ...

  8. Qt 外观之一 ——Qt Style Sheet

    Qt Style Sheet 目录 使用 对于应用程序 创建自定义控件 QSS语法 一般选择器(selector) 伪选择器 解决冲突 使用specificity Namespace冲突 级联效应 设 ...

  9. 使用Qt installer framework制作安装包

    一.介绍 使用Qt库开发的应用程序,一般有两种发布方式:(1)静态编译发布.这种方式使得程序在编译的时候会将Qt核心库全部编译到一个可执行文件中.其优势是简单单一,所有的依赖库都集中在一起,其缺点也很 ...

随机推荐

  1. centos7启动时出现“无法应用原保存的显示器配置”

    设置了分辨率后,登录提示“出现无法应用原保存的显示器配置”. 解决办法: 打开终端,输入 rm ~/.config/monitors.xml 然后重新登录, 问题解决.

  2. Tree( 树) 组件[2]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 异步加载如果想从数据库里获取导航内容, 那么就必须 ...

  3. 解决操作过快导致ajax异常的办法

    //控制点击过快ajax异常 var state = true; function test() { if (state) { state = false; var val = accMul((uCo ...

  4. Win8 弹出窗口不在最前端的解决方法

    Win8系统的使用者有很多会遇到弹出窗口不在最前端的情况(自动隐藏,点下页面又出来),比如另存为的时候 ,或是登录路由器时弹出的登录框时. 引起这个异常的原因是与系统输入法冲突引起,但又不可能不用第三 ...

  5. hdu 2101

    #include <stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { i ...

  6. 关于winform主题IrisSkin2的编写

    第一步:首先引用IrisSkin2.dll. 第二步自定义类: /// <summary> /// 窗体主题边界类 /// </summary> public class Fo ...

  7. 关于html页面图片自动撑开的问题

    如下列代码: <div id="divnr" style="text-align: center; margin: 10px; width:600px;" ...

  8. FineUI 点击按钮添加标签页

    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...

  9. iOS调节系统音量

    目录[-] 使用MPVolumeView 编程实现系统音量调节2 通过MPVolumeSlider的实例来操作系统音量 有问题!我不喜欢系统弹出音量提示 还有问题,我修改了系统音量但是不是通过我的UI ...

  10. nginx 配置文件解析(一)

    nginx.conf user nginx; # nginx服务的运行用户 worker_processes ; # 启动进程数,通常设置成和CPU的数量相等 error_log /var/log/n ...