#ifndef QT_SHAPEDWINDOW_H
#define QT_SHAPEDWINDOW_H
#include <QPainter>
#include <QtWidgets/QMainWindow>
#pragma execution_character_set("utf-8")
class Qt_Shapedwindow : public QMainWindow
{
Q_OBJECT
public:
Qt_Shapedwindow(QWidget *parent = 0);
~Qt_Shapedwindow();
private:
void paintEvent(QPaintEvent *);
};
#endif // QT_SHAPEDWINDOW_H
#include "qt_shapedwindow.h"
#include <QtWidgets/QPushButton>
Qt_Shapedwindow::Qt_Shapedwindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowOpacity(1); //窗口整体透明度,0-1 从全透明到不透明
this->setWindowFlags(Qt::FramelessWindowHint); //设置无边框风格
this->setAttribute(Qt::WA_TranslucentBackground); //设置背景透明,允许鼠标穿透
resize(1000, 666);
QPixmap pixmap;
pixmap.load("f:\\nn\\按钮1.png");
int nWidth = pixmap.width() * 2;
int nHeight = pixmap.height() * 2;
QPushButton * pBtn = new QPushButton(pixmap, "", this);
pBtn->resize(pixmap.width(), pixmap.height());
pBtn->move(1000 - pixmap.width(), 0);
connect(pBtn, SIGNAL(clicked()), this, SLOT(close()));
}
Qt_Shapedwindow::~Qt_Shapedwindow()
{
}
void Qt_Shapedwindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
QPixmap pix;
pix.load("f:\\nn\\123.png");
p.drawPixmap(0, 0, pix);
}
#include "qt_shapedwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Qt_Shapedwindow w;
w.show();
return a.exec();
}
运行结果: