您的位置 首页 培训

QT完成不规则窗体

看到网上有很多不规则窗体的实现,效果很酷.于是使用QT也实现了一个,QT的不规则窗体实现非常简单,只需要设置一个mask(遮掩)图片,这个图片的格式可以

看到网上有许多不规则窗体的完成,作用很帅.所以运用QT也完成了一个,QT不规则窗体完成十分简略,只需求设置一个mask(讳饰)图片,这个图片的格局能够运用png或bmp格局,我运用了png格局,默许窗体是矩形的,运用png图画,将需求阻隔在窗体之外的区域的像素设置为白色或通明色,其他色彩的区域对应显示出来的窗体.要害代码就几行.

#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())); //设置窗体的掩码图画,抠除图画的白色区域完成不规则窗体

QPalette p;//设置调色板

p.setBrush(QPalette::Window, QBrush(mask));//将调色板的画刷设置为掩码位图,在不规则窗体上显示出掩码位图

setPalette(p);

mouseMovePos = QPoint(0, 0);

}

IrregularForm::~IrregularForm()

{

}

void IrregularForm::mouseMoveEvent(QMouseEvent *event)//鼠标按下并移动则移动不规则窗体

{

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);

}

作用图

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/peixun/297349.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部