QT實現(xiàn)不規(guī)則窗體
看到網(wǎng)上有很多不規(guī)則窗體的實現(xiàn),效果很酷.于是使用QT也實現(xiàn)了一個,QT的不規(guī)則窗體實現(xiàn)非常簡單,只需要設(shè)置一個mask(遮掩)圖片,這個圖片的格式可以使用png或bmp格式,我使用了png格式,默認(rèn)窗體是矩形的,使用png圖像,將需要隔離在窗體之外的區(qū)域的像素設(shè)置為白色或透明色,其他顏色的區(qū)域?qū)?yīng)顯示出來的窗體.關(guān)鍵代碼就幾行.
本文引用地址:http://m.butianyuan.cn/article/201610/305993.htm#ifndef IRREGULARFORM_H
#define IRREGULARFORM_H
#include
#include ui_irregularform.h
#include
#include
#include
#include
class IrregularForm : public QWidget
{
Q_OBJECT
public:
IrregularForm(QWidget *parent = 0);
~IrregularForm();
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
Ui::IrregularFormClass ui;
QPoint mouseMovePos;
};
#endif // IRREGULARFORM_H
#include irregularform.h
IrregularForm::IrregularForm(QWidget *parent)
: QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
QPixmap mask(:/IrregularForm/Resources/mask.png);//加載掩碼圖像
setMask(QBitmap(mask.mask())); //設(shè)置窗體的掩碼圖像,摳除圖像的白色區(qū)域?qū)崿F(xiàn)不規(guī)則窗體
QPalette p;//設(shè)置調(diào)色板
p.setBrush(QPalette::Window, QBrush(mask));//將調(diào)色板的畫刷設(shè)置為掩碼位圖,在不規(guī)則窗體上顯示出掩碼位圖
setPalette(p);
mouseMovePos = QPoint(0, 0);
}
IrregularForm::~IrregularForm()
{
}
void IrregularForm::mouseMoveEvent(QMouseEvent *event)//鼠標(biāo)按下并移動則移動不規(guī)則窗體
{
if(mouseMovePos != QPoint(0, 0))
{
move(geometry().x() + event->globalPos().x() - mouseMovePos.x(), geometry().y() + event->globalPos().y() - mouseMovePos.y());
mouseMovePos = event->globalPos();
}
}
void IrregularForm::mousePressEvent(QMouseEvent *event)
{
mouseMovePos = event->globalPos();
}
void IrregularForm::mouseReleaseEvent(QMouseEvent *event)
{
mouseMovePos = QPoint(0, 0);
}
效果圖
評論