標籤:fse event ops aspect 需要 ase div sep blur
很多人覺得系統內建的標題列太醜了,想要自繪一個標題列,去掉了系統內建的標題列後,就需要自己實現視窗移動,下面的代碼就是實現視窗移動。
widget.h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QPoint>#include <QMouseEvent> //引用滑鼠類標頭檔class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); QPoint last; //滑鼠按下 void mousePressEvent(QMouseEvent *e); //滑鼠移動 void mouseMoveEvent(QMouseEvent *e); //滑鼠釋放 void mouseReleaseEvent(QMouseEvent *e);signals:public slots:};#endif // WIDGET_H
widget.cpp
#include "Widget.h"#include <QApplication>#include <QLabel>#include <QTimer>#include <QMovie>#include <QImage>#include <QGraphicsBlurEffect> //模糊效果#include <QGraphicsColorizeEffect> //著色效果#include <QGraphicsDropShadowEffect> //陰影製作效果#include <QGraphicsOpacityEffect> //透明效果Widget::Widget(QWidget *parent) : QWidget(parent){ QLabel* label = new QLabel(this);#if 0 label->setGeometry(0,0,200,200); QMovie* mv = new QMovie("../a.PNG"); label->setMovie(mv); //3秒後圖片消失 QTimer::singleShot( 3*1000, label, SLOT(close())); //圖片移動 label->move(200,200); mv->start();#endif QImage *img = new QImage; img->load("../a.PNG"); //QImage 載入圖片#if 0 //水平翻轉 *img = img->mirrored(true,false); //垂直翻轉 *img = img->mirrored(false,true); //旋轉60度角 QMatrix matrix; *img = img->transformed(matrix.rotate(60)); // 重新設定圖片大小 *img = img->scaled(1000,1000,Qt::IgnoreAspectRatio);#endif#if 0 QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this); effect->setBlurRadius(3); //模糊值,值越大越模糊 QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(this); effect->setColor(QColor(0,200,0));//著色 QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this); //設定透明值 effect->setOpacity(0.5);//透明效果 /* OuterGlowBitmapEffect(外光暈) DropShadowBitmapEffect(陰影) EmbossBitmapEffect(浮雕) BlurBitmapEffect(模糊) BevelBitmapEffect(斜角)。 */#endif#if 1 QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this); //設定陰影 effect->setOffset(100,400); effect->setColor(QColor(20,20,20)); effect->setBlurRadius(500);#endif label->setGraphicsEffect(effect);//設定label效果 label->setPixmap(QPixmap::fromImage(*img));//把圖片載入到label}//滑鼠按下void Widget::mousePressEvent(QMouseEvent *e){ if(e->pos().rx()>1000 || e->pos().ry()>500) return ; last = e->globalPos();}//滑鼠移動void Widget::mouseMoveEvent(QMouseEvent *e){ if(e->pos().rx()>1000 || e->pos().ry()>500) return ; int dx = e->globalX() - last.x(); int dy = e->globalY() - last.y(); last = e->globalPos(); move(x()+dx,y()+dy);}//滑鼠釋放void Widget::mouseReleaseEvent (QMouseEvent *e){ if(e->pos().rx()>1000 || e->pos().ry()>500) return ; int dx = e->globalX() - last.x(); int dy = e->globalY() - last.y(); move(x()+dx, y()+dy);}int main(int argc,char** argv){ QApplication app(argc,argv); Widget w; //去掉標題列 w.setWindowFlags(Qt::FramelessWindowHint); //設定背景透明 //w.setAttribute(Qt::WA_TranslucentBackground, true); //固定大小 w.setGeometry(300,200,1000,500); w.show(); return app.exec();}
Qt 視窗移動實現