Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)
容易搞定,mainWindow 是一个QWidget。
// 设置背景色为蓝色
mainWindow.setStyleSheet("background-color:blue;");
// 设置背景图片为bg.png
mainWindow.setObjectName("mainWindow");
mainWindow.setStyleSheet("#mainWindow{border-image:url(:/image/bg.png);}");
这里要注意,ObjectName 并非可有可无,如果不使用它会导致该widget 的所有子控件也会有相同的背景。不使用"background-image"的理由是"border-image"可以自动填充。
如果想恢复默认背景,可以调用
mainWindow.setStyleSheet(0);
:/image/bg.png 前面有冒号说明它是qrc 中定义的资源;顺便说一下如何添加和编辑qrc 文件。
在工程上右键点击“Add New...”,然后 => "Qt" => "Qt Resource File" ,随便起个名字 => "Next" => "Finish",添加qrc 文件完毕。
接下来添加图片资源,选中刚才添加的qrc 文件,"Add Prefix",建议把"Prefix" 设为"/";然后"Add Files",把图片添加到资源文件中。
当然,也可以从磁盘上直接载入图片。与使用资源文件中定义的图片相比,时间开销大,但空间开销小,同时也更灵活一些,例如显示下载的图片。
mainWindow.setObjectName("mainWindow");
mainWindow.setStyleSheet("#mainWindow{border-image:url(c:/downloads/myapp/image/bg.png);}");
http://blog.csdn.net/itjobtxq/article/details/9284415
Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)的更多相关文章
- QT:给Widget设置背景图片——设置Widget的调色板,调色板使用图片和背景色
QT:给Widget设置背景图片 1 /*2 * set background image3 */4 QPixmap bgImages(":/images/bg.png");5 Q ...
- Qt添加窗口背景图片、Label图片显示、、Label文字显示
一.添加窗口背景图片 重写MainWindow绘制事件 void MainWindow::paintEvent(QPaintEvent *event) { QPainter painter(this) ...
- 在Vue项目中,添加的背景图片在服务器上不显示,如何处理
遇到的问题: 在vue项目开发过程中,我们常常会在页面中添加背景图片.可是当我们在样式中添加了背景图片,编译打包部署到服务器上时,发现图片并不能显示出来,这是为什么呢~~~ 我们一般写的css样式如下 ...
- CSS 基础 例子 背景色 & 背景图片
背景简写形式 : body {background:#ffffff url('img_tree.png') no-repeat right top;} 一.背景色 background-color ...
- CSS再学习 之背景色 背景图片
背景色 p {background-color: gray;} 如果您希望背景色从元素中的文本向外少有延伸,只需增加一些内边距. p {background-color: gray; padding: ...
- android红米等关于读取本地文件夹图片获取路径的问题的解决
在Android开发中,有从本地文件夹中读取图片的功能,使用一下代码打开图片选择列表: Intent intent = new Intent(); intent.setAction(Intent. ...
- vue 动态添加body背景图片
<script> export default { data () { return { bodyBgImage: 'url(' + require('../asse ...
- MFC实现为窗体添加的背景图片
将一个bmp图片添加到资源中 在资源视图中更改位图资源的ID为IDB_BITMAP_BACKGROUND. 第一种方法: 在Dialog中添加一个Picture Control控件,将Picture ...
- mfc中 控件 对话框 添加颜色 背景图片
1 设置对话框透明 在设置控件颜色中要使用 nCtlColor Contains one of the following values, specifying the type of control ...
随机推荐
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...
- Windows API中几个函数的总结
[DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr ...
- SASS组件开发
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 线段树讲解(数据结构、C++)
声明 : 仅一张图片转载于http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464583.html,自己画太麻烦了...那个博客的讲解也很好 ...
- servlet三种实现方式之三通过继承HttpServlet开发servlet
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第三种: import java.io.*; i ...
- hrtimer 高精定时器使用例子
在kernel,有个线程每个十秒往上层系统上报battery及USB的状态信息. 加入直接sleep 10 秒的话,插入USB的时候不能及时更新状态,这个时候就可以使用 hrtimer + wait ...
- nrf51 官方PWM库
地址:https://github.com/NordicSemiconductor/nrf51-pwm-library nrf_pwm_init函数 初始化PWM参数 设置输出pwm的gpio pin ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- Win32 GDI基础(笔记)
1.GDI名字的意义 GDI Graphic Device Interface,我说不清和GUI有什么区别.可能一种针对设备,一种针对用户而言吧,反正以后都说GDI,也就是Windows的图形编程. ...
- T-SQL函数及用法--转
转自http://www.cnblogs.com/qixuejia/archive/2010/07/14/1777105.html 1. 聚合函数 (1) AVG 函数功能返回组中值的平均值.空值将被 ...