Qt异形窗口
首先把窗口大小调整为图片大小,设置窗口无标题栏,然后加上鼠标拖动的代码:
setWindowFlags(Qt::FramelessWindowHint); //去掉标题栏
m_pixmap.load(":/img/plant.png"); //加载
resize(m_pixmap.size()); //设置窗口大小为图片大小
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_ptPress = event->globalPos(); //记录按下位置
m_bLeftBtnPress = true;
}
event->ignore();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(m_bLeftBtnPress) //移动窗口
{
move(pos() + event->globalPos() - m_ptPress);
m_ptPress = event->globalPos();
}
event->ignore();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
m_bLeftBtnPress = false;
event->ignore();
}
接下来设置异形窗口。
- 方法一:
初始化时:
// 方法一:
setAutoFillBackground(true); //设置自动填充
setMask(m_pixmap.mask()); //设置图片透明的地方为穿透
重写重绘函数:
void MainWindow::paintEvent(QPaintEvent *) //绘制
{
QPalette palette = this->palette();
palette.setBrush(QPalette::Background,m_pixmap);
setPalette(palette);
}
效果:
但是图片中不是半透明的部分就会变成黑色,找半天找不到原因。
- 方法二:
通过设置窗口样式实现,需要将UI中的状态栏去掉,初始化时:
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("image: url(:/img/plant.png);");
效果:
- 完整代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QBitmap>
#include <QPalette>
#include <QMouseEvent>
#include <QPoint>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void paintEvent(QPaintEvent * event);
protected:
void mouseMoveEvent(QMouseEvent * event);
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
private:
Ui::MainWindow *ui;
QPixmap m_pixmap;
//设置拖动
QPoint m_ptPress;
bool m_bLeftBtnPress;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include <windows.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
m_bLeftBtnPress = false;
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint); //去掉标题栏
m_pixmap.load(":/img/plant.png"); //加载
resize(m_pixmap.size()); //设置窗口大小为图片大小
// 方法一:
/*
setAutoFillBackground(true); //设置自动填充
setMask(m_pixmap.mask()); //设置图片透明的地方为穿透
*/
// 方法二:
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("image: url(:/img/plant.png);");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *) //绘制
{
/*
QPalette palette = this->palette();
palette.setBrush(QPalette::Background,m_pixmap);
setPalette(palette);
*/
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_ptPress = event->globalPos(); //记录按下位置
m_bLeftBtnPress = true;
}
event->ignore();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(m_bLeftBtnPress) //移动窗口
{
move(pos() + event->globalPos() - m_ptPress);
m_ptPress = event->globalPos();
}
event->ignore();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
m_bLeftBtnPress = false;
event->ignore();
}
代码:http://download.youkuaiyun.com/download/yangyang031213/10126036
https://github.com/yangyang0312/cpp/tree/master/Qt/ProfiledWindow