本代码通过选择文件导入图片,然后用GraphicsView显示,点击开始动画(渐隐,渐显,变小,变大,旋转)graphiCSSpeak.h#ifndef GRAPHICSITEM_H#define GRAPHICSITEM_H#include <QGraphicsPixmapItem>#include <QGraphicsWidget>#include <QPainter>#include <QRect>#include <QPixmap>#include <QWidget>#include <QDebug>#include <QPRopertyAnimation>#include <QObject>#include <QWindow>class GraphicsItem : public QGraphicsWidget{ Q_OBJECT Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry) Q_PROPERTY(QPointF pos READ pos WRITE setPos) Q_PROPERTY(qreal rotation READ rotation WRITE setRotation) Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)public: GraphicsItem(QPixmap *pixmap); QRectF boundingRect() const; void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget);private: QRect my_rect; QPixmap *my_Pixmap;};#endif // GRAPHICSITEM_HgraphicsSpeak.cpp#include "graphicsitem.h"GraphicsItem::GraphicsItem(QPixmap *pixmap){ my_Pixmap = new QPixmap(); my_Pixmap = pixmap;}QRectF GraphicsItem::boundingRect() const{ return QRectF(-this->rect().width()/2,-this->rect().height()/2,this->rect().width(),this->rect().height());}void GraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ painter->drawPixmap(boundingRect().toRect(),*my_Pixmap);}MainWindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QGraphicsView>#include <QGraphicsScene>#include <QGraphicsWidget>#include <QPropertyAnimation>#include <QMouseEvent>#include <QFileDialog>#include <QMessageBox>#include "graphicsitem.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();protected: void mousePressEvent(QMouseEvent *event);private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked();private: Ui::MainWindow *ui; GraphicsItem *my_graphicsItem; QPropertyAnimation *animation1; QPropertyAnimation *animation2; QPropertyAnimation *animation3; QGraphicsScene *scene; QPixmap pixmap;};#endif // MAINWINDOW_HMainWindiw.cpp#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); scene = new QGraphicsScene(); scene->setBackgroundBrush(Qt::black); ui->graphicsView->setScene(scene); ui->pushButton->setToolTip("选择文件"); animation1 = new QPropertyAnimation(); animation2 = new QPropertyAnimation(); animation3 = new QPropertyAnimation(); animation1->setDuration(3000); animation1->setStartValue(360); animation1->setEndValue(0); animation1->setLoopCount(-1); animation3->setDuration(3000); animation3->setStartValue(0); animation3->setEndValue(1); animation2->setDuration(5000); animation2->setStartValue(QRect(0,0,97,46)); animation2->setKeyValueAt(0.5,QRect(0,0,97*4,46*4)); animation2->setEndValue(QRect(-550,-300,97,46)); connect(animation2,SIGNAL(finished()),animation1,SLOT(start()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::mousePressEvent(QMouseEvent *event){ if(animation2 != NULL&&animation2->state() != QAbstractAnimation::Running){ animation2->start(); }}void MainWindow::on_pushButton_clicked(){ QString path = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files( *.png)")); if(!path.isEmpty()) { ui->lineEdit->setText(path); } else{ QMessageBox::warning(NULL,"waring","请选择图片",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes); }}void MainWindow::on_pushButton_2_clicked(){ pixmap.load(ui->lineEdit->text()); my_graphicsItem = new GraphicsItem(&pixmap); scene->addItem(my_graphicsItem); animation1->setTargetObject(my_graphicsItem); animation2->setTargetObject(my_graphicsItem); animation3->setTargetObject(my_graphicsItem); animation1->setPropertyName("rotation"); animation2->setPropertyName("geometry"); animation3->setPropertyName("opacity"); animation3->start();}main.cpp#include "mainwindow.h"#include <Qapplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
新闻热点
疑难解答