Qt: 给Widget设置背景图片
来源:互联网 发布:手机淘宝网app下载 编辑:程序博客网 时间:2024/04/30 10:12
1. QPalette的方法
#include <QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QFrame *frame = new QFrame;
frame->resize(400,700);
QPixmap pixmap("images/frame.png");
QPalette palette;
palette.setBrush(frame->backgroundRole(),QBrush(pixmap));
frame->setPalette(palette);
frame->setMask(pixmap.mask()); //可以将图片中透明部分显示为透明的
frame->setAutoFillBackground(true);
frame->show();
return app.exec();
}
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QFrame *frame = new QFrame;
frame->resize(400,700);
QPixmap pixmap("images/frame.png");
QPalette palette;
palette.setBrush(frame->backgroundRole(),QBrush(pixmap));
frame->setPalette(palette);
frame->setMask(pixmap.mask()); //可以将图片中透明部分显示为透明的
frame->setAutoFillBackground(true);
frame->show();
return app.exec();
}
2.setStyleSheet方法
#include <QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QFrame *frame = new QFrame;
frame->setObjectName("myframe");
frame->resize(400,700);
frame->setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}" );
frame->show();
return app.exec();
}
注意代码中红线的部分噢,设置ObjectName后,才能保证setStyleSheet只作用在我们的frame上,不影响其子控件的背景设置。之所以用border-image而不用background-image,还是上面的问题,用background-image不能保证图片大小和控件大小一致,图片不能完全显示。#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QFrame *frame = new QFrame;
frame->setObjectName("myframe");
frame->resize(400,700);
frame->setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}" );
frame->show();
return app.exec();
}
摘自Qt官方手册中的Customizing Qt Widgets Using Style Sheets章节的一小段:
A background-image does not scale with the size of the widget. To provide a "skin" or background that scales along with the widget size, one must use border-image. Since the border-image property provides an alternate background, it is not required to specify a background-image when border-image is specified. In the case, when both of them are specified, the border-image draws over the background-image.
3.paintEvent事件方法默认background-image 不会缩放图片以适应控件的大小。如果要提供一个皮肤或背景图片以自动适应控件大小,必须也只能用border-image属性。因为border-image已经设置了可用的背景图片,所以使用了border-image后,没必要再指定background-image。如果同时指定了两个属性,那么将会使用border-image 绘制覆盖掉background-image。
//myframe.h文件
#ifndef MYFRAME_H
#define MYFRAME_H
#include <QWidget>
#include <QtGui>
class MyFrame : public QWidget
{
public:
MyFrame();
void paintEvent(QPaintEvent *event);
};
#endif // MYFRAME_H
//myframe.cpp文件
#include "myframe.h"
MyFrame::MyFrame()
{
}
void MyFrame::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,400,700,QPixmap("images/frame.png"));
}
//main.cpp文件
#include <QApplication>
#include <QtGui>
#include "myframe.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
MyFrame *frame = new MyFrame;
frame->resize(400,700);
frame->show();
return app.exec();
}
#ifndef MYFRAME_H
#define MYFRAME_H
#include <QWidget>
#include <QtGui>
class MyFrame : public QWidget
{
public:
MyFrame();
void paintEvent(QPaintEvent *event);
};
#endif // MYFRAME_H
//myframe.cpp文件
#include "myframe.h"
MyFrame::MyFrame()
{
}
void MyFrame::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,400,700,QPixmap("images/frame.png"));
}
//main.cpp文件
#include <QApplication>
#include <QtGui>
#include "myframe.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
MyFrame *frame = new MyFrame;
frame->resize(400,700);
frame->show();
return app.exec();
}
转自:http://www.cppblog.com/qianqian/archive/2010/07/25/121238.html
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt: 给Widget设置背景图片
- Qt 如何 给Widget设置背景图片
- Qt中: 给Widget设置背景图片
- Qt 如何 给Widget设置背景图片
- Qt 如何给Widget设置背景图片
- 给Widget设置背景图片
- QT 给WIdget设置 背景图片和背景颜色
- QT 自定义widget 背景图片设置
- Qt中设置widget背景颜色背景图片的注意事项
- Qt中设置widget背景颜色背景图片的注意事项
- 小心使用zeromq
- frameworksssss
- 正则表达式
- kdesvn中删除文件夹后commit出现Aborting commit: 'pdu/rootfs/busybox-1.15.3' remains in tree-conflict错误
- 关于"未能映射路径"问题
- Qt: 给Widget设置背景图片
- 如何用servlet/jsp实现在线用户统计
- Sort Table
- 不错的电子书籍下载网址
- NDK Makefile文件中LOCAL_LDLIBS和LOCAL_CFLAGS介绍
- Linux时间函数之gettimeofday()函数之使用方法
- js 打印页面局部内容
- linux下错误的捕获:errno和strerror的使用
- java.lang.IllegalArgumentException: Can't convert argument: null