Qt 視窗移動實現

來源:互聯網
上載者:User

標籤: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 視窗移動實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.