新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > QT實(shí)現(xiàn)不規(guī)則窗體

QT實(shí)現(xiàn)不規(guī)則窗體

作者: 時(shí)間:2016-10-08 來(lái)源:網(wǎng)絡(luò) 收藏

看到網(wǎng)上有很多的實(shí)現(xiàn),效果很酷.于是使用也實(shí)現(xiàn)了一個(gè),實(shí)現(xiàn)非常簡(jiǎn)單,只需要設(shè)置一個(gè)mask(遮掩)圖片,這個(gè)圖片的格式可以使用png或bmp格式,我使用了png格式,默認(rèn)窗體是矩形的,使用png圖像,將需要隔離在窗體之外的區(qū)域的像素設(shè)置為白色或透明色,其他顏色的區(qū)域?qū)?yīng)顯示出來(lái)的窗體.關(guān)鍵代碼就幾行.

本文引用地址:http://www.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)

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)按下并移動(dòng)則移動(dòng)不規(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);

}

效果圖



關(guān)鍵詞: QT 不規(guī)則窗體

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉