shapewidget.h
#ifndef SHAPEWIDGET_H
#define SHAPEWIDGET_H
#include <QtGui>
class QPoint;
class ShapeWidget : public QWidget
{
Q_OBJECT
public:
ShapeWidget(QWidget *parent = 0, Qt::WFlags flags = 0);
~ShapeWidget();
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
private:
QPoint dragPosition;
};
#endif // SHAPEWIDGET_H
shapewidget.cpp
#include "shapewidget.h"
#include <QPixmap>
#include <QBitmap>
ShapeWidget::ShapeWidget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
QPixmap pix; //執行個體化一個位圖對象
pix.load("images/tux.png",
0,
Qt::AvoidDither| Qt::ThresholdDither | Qt::ThresholdAlphaDither); //加載掩碼圖像
resize(pix.size()); //設定圖像的大小
setMask(pix.mask()); //摳除圖像的白色區域實作不規則窗體
QPalette plt; //執行個體化調色闆
plt.setBrush(QPalette::Window, QBrush(pix)); //将調色闆的畫刷設定為掩碼位圖,在不規則窗體上顯示出掩碼位圖
setPalette(plt); //按調色闆資訊設定窗體
}
ShapeWidget::~ShapeWidget()
void ShapeWidget::mousePressEvent(QMouseEvent * event)
if (event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
if (event->button() == Qt::RightButton)
close();
void ShapeWidget::mouseMoveEvent(QMouseEvent * event)
if (event->buttons() & Qt::LeftButton)
move(event->globalPos() - dragPosition);
shapewidget.cpp中第30行的frameGeometry().topLeft()仍然表示整個窗體的左上角,而不是所見不規則窗體的左上角。
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。
本文轉自韓兆新部落格部落格園部落格,原文連結:http://www.cnblogs.com/hanzhaoxin/archive/2012/11/22/2783373.html,如需轉載請自行聯系原作者