#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 <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();
}
//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();
}
效果如下: