Qtワークノート-3 D効果はQWidgetsインタフェースを呼び出します(QGraphicsProxyWidgetとQTimeLine)

6757 ワード

プログラムの実行断図は以下の通りです.
 
キー1:
->この矢印は左のクラスが右のクラスに入っていることを示しています
QWidget->QGraphicsProxyWidget
QGraphicsProxyWidget->QGraphicsScene
QGraphicsScene->QGraphicsView
このプログラムでUIでQGraphicsViewはQWIdgetに入れています
QGraphicsProxyWidgetについてのメモをご覧ください.
https://blog.csdn.net/qq78442761/article/details/81507130
 
キー2:
QTimeLineはQtの時間線関数であり,線には両端があるため2方向があり,一般的にQTimeLineを用いてこのような生き生きとした回転を実現する.
 
ソースコードは次のとおりです.
mycustomproxy.h
#ifndef MYCUSTOMPROXY_H
#define MYCUSTOMPROXY_H

#include 
#include 

QT_BEGIN_NAMESPACE
class QGraphicsSceneHoverEvent;
QT_END_NAMESPACE

class MyCustomProxy : public QGraphicsProxyWidget
{
    Q_OBJECT
public:
    explicit MyCustomProxy(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
    ~MyCustomProxy();

protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;

protected slots:
    void updateStep(int step);
    void finished();

private:
    QTimeLine *m_timeLine;
    qreal m_currAngle;
};

#endif // MYCUSTOMPROXY_H

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 

QT_BEGIN_NAMESPACE
class QGraphicsScene;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;

    QGraphicsScene *m_scene;
};

#endif // WIDGET_H

widgetwindow.h
#ifndef WIDGETWINDOW_H
#define WIDGETWINDOW_H

#include 

namespace Ui {
class WidgetWindow;
}

class WidgetWindow : public QWidget
{
    Q_OBJECT

public:
    explicit WidgetWindow(QWidget *parent = 0);
    ~WidgetWindow();

protected slots:
    void btnClicked();

private:
    Ui::WidgetWindow *ui;
};

#endif // WIDGETWINDOW_H

main.cpp
#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

mycustomproxy.cpp
#include "mycustomproxy.h"
#include 
#include 
#include 

MyCustomProxy::MyCustomProxy(QGraphicsItem *parent, Qt::WindowFlags wFlags)
    : QGraphicsProxyWidget(parent,wFlags)
{
    QTransform transform;
    setTransform(transform.rotate(-60,Qt::YAxis));
    m_currAngle=0;

    m_timeLine=new QTimeLine;
    m_timeLine->setFrameRange(-60,0);
    connect(m_timeLine,SIGNAL(frameChanged(int)),this,SLOT(updateStep(int)));
    connect(m_timeLine,SIGNAL(finished()),this,SLOT(finished()));
}

MyCustomProxy::~MyCustomProxy()
{

}

void MyCustomProxy::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    scene()->setActiveWindow(this);

    m_timeLine->setDirection(QTimeLine::Forward);

    if(m_timeLine->state()!=QTimeLine::Running)
        m_timeLine->start();

    QGraphicsProxyWidget::hoverEnterEvent(event);
}

void MyCustomProxy::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    m_timeLine->setDirection(QTimeLine::Backward);

    if(m_timeLine->state()!=QTimeLine::Running)
        m_timeLine->start();

    QGraphicsProxyWidget::hoverLeaveEvent(event);
}

void MyCustomProxy::updateStep(int step)
{
    //qDebug()<

widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "widgetwindow.h"
#include "mycustomproxy.h"
#include 
#include 
#include 


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->graphicsView->setStyleSheet("background: transparent;border:0px");
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setRenderHints(ui->graphicsView->renderHints()|QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    m_scene=new QGraphicsScene;
    ui->graphicsView->setScene(m_scene);

    MyCustomProxy *myCustomProxy_1=new MyCustomProxy;
    myCustomProxy_1->setWidget(new WidgetWindow);
    myCustomProxy_1->setWindowFlags(Qt::Desktop);
    myCustomProxy_1->setWindowTitle("QWidget demo   ");
    myCustomProxy_1->setPos(-300,0);

    MyCustomProxy *myCustomProxy_2=new MyCustomProxy;
    myCustomProxy_2->setWidget(new WidgetWindow);
    myCustomProxy_2->setWindowFlags(Qt::Desktop);
    myCustomProxy_2->setWindowTitle("QWidget demo   ");
    myCustomProxy_2->setPos(-150,0);

    MyCustomProxy *myCustomProxy_3=new MyCustomProxy;
    myCustomProxy_3->setWidget(new WidgetWindow);
    myCustomProxy_3->setWindowFlags(Qt::Desktop);
    myCustomProxy_3->setWindowTitle("QWidget demo   ");
    myCustomProxy_3->setPos(0,0);

    MyCustomProxy *myCustomProxy_4=new MyCustomProxy;
    myCustomProxy_4->setWidget(new WidgetWindow);
    myCustomProxy_4->setWindowFlags(Qt::Desktop);
    myCustomProxy_4->setWindowTitle("QWidget demo   ");
    myCustomProxy_4->setPos(150,0);

    MyCustomProxy *myCustomProxy_5=new MyCustomProxy;
    myCustomProxy_5->setWidget(new WidgetWindow);
    myCustomProxy_5->setWindowFlags(Qt::Desktop);
    myCustomProxy_5->setWindowTitle("QWidget demo   ");
    myCustomProxy_5->setPos(300,0);

    m_scene->addItem(myCustomProxy_1);
    m_scene->addItem(myCustomProxy_2);
    m_scene->addItem(myCustomProxy_3);
    m_scene->addItem(myCustomProxy_4);
    m_scene->addItem(myCustomProxy_5);

    ui->graphicsView->setSceneRect(-404.027,-40.2044,939.058,402.725);
    ui->graphicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
}

Widget::~Widget()
{
    delete ui;
}

widgetwindow.cpp
#include "widgetwindow.h"
#include "ui_widgetwindow.h"
#include 

WidgetWindow::WidgetWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
}

WidgetWindow::~WidgetWindow()
{
    delete ui;
}

void WidgetWindow::btnClicked()
{
    QMessageBox::information(this,"  ","      !");
}