Qt 场景QGraphicsView显示多个包括按键图片的集合,使用动画实现居中过渡,支持两种显示模式带渐变通明

场景中显示的集合体包括按键图片

#ifndef NORMALMENU_H
#define NORMALMENU_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>

class NormalMenu : public QFrame
{
    Q_OBJECT

public:
    NormalMenu(QWidget *parent = 0);
    void setTestName(QString name);
    void setPicture(QString pictureDir);
    ~NormalMenu();
private:
    QHBoxLayout *horlayout;
    QVBoxLayout *Vlayout;
    QLabel *imageLabel;
    QLabel *testName;
    QPushButton *esaybutton;
    QPushButton *multbutton;
    QPushButton *standardbutton;
    QPushButton *mytestbutton;
    QSpacerItem *horizontalSpacer;
};

#endif // WIDGET_H

#include "NormalMenu.h"

NormalMenu::NormalMenu(QWidget *parent)
    : QFrame(parent)
{
    QHBoxLayout *horlayout = new QHBoxLayout();
    QVBoxLayout *Vlayout =new QVBoxLayout();
    QLabel *testName = new QLabel();
    testName->setText("Surge");
    testName->setStyleSheet("QLabel {border: none;"
                        "background-color: black;"
                        "}");
    testName->setAlignment(Qt::AlignCenter);
    QPushButton *esaybutton = new QPushButton();
    QPushButton *multbutton = new QPushButton();
    QPushButton *standardbutton = new QPushButton();
    QPushButton *mytestbutton = new QPushButton();
    QSpacerItem *horizontalSpacer;
    QString stylebutton("QPushButton {border: 2px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                        "stop: 0 #766679, stop: 1 grey);"
                         "background-color: black;"
                         "border-bottom-color: grey;"
                         "color: white;"
                         "min-height: 30px;"
                         "min-width: 100px}"
                        "QPushButton:hover{border: 2px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                        "stop: 0 #D2691E, stop: 1 grey);"
                        "border-bottom-color: grey;"
                        "background-color: black;"
                        "color: white;}"
                        "QPushButton::pressed{border: 2px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                        "stop: 0 #c1c11e, stop: 1 white);"
                        "border-bottom-color: grey;"
                        "background-color: black;"
                        "color: white;}");
    esaybutton->setStyleSheet(stylebutton);
    multbutton->setStyleSheet(stylebutton);
    standardbutton->setStyleSheet(stylebutton);
    mytestbutton->setStyleSheet(stylebutton);

//    QWidget     *mywidget = new QWidget();
    horizontalSpacer = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    QLabel *imageLabel = new QLabel();
    imageLabel->setStyleSheet("QLabel {border: none;"
                               "background-color: black;"
                               "}");
    imageLabel->setAlignment(Qt::AlignCenter);

//    imageLabel->setPixmap(QPixmap("://image/test1.png").scaled(QSize(100,80)));

    esaybutton->setText("EsayTest");
    multbutton->setText("MultTest");
    standardbutton->setText("Standard");
    mytestbutton->setText("MyTest");
    horlayout->addWidget(standardbutton);
    horlayout->addItem(horizontalSpacer);
    horlayout->addWidget(mytestbutton);
    horlayout->setSpacing(10);

    Vlayout->addWidget(imageLabel);
    Vlayout->addWidget(testName);
    Vlayout->addWidget(esaybutton);
    Vlayout->addWidget(multbutton);
    Vlayout->addLayout(horlayout);
    Vlayout->setSpacing(10);
    setLayout(Vlayout);
    setStyleSheet("QWidget{border: 4px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                            "stop: 0 #ecb013, stop: 1 grey);"
                            "border-bottom-color: grey;"
                            "border-radius: 8px;"
                            "background-color: black;"
                            "color: white;""}");
//    setAttribute(Qt::WA_NoSystemBackground);
//    resize(160,380);
    setMinimumSize(80,100);
}

void NormalMenu::setTestName(QString name)
{
    testName->setText(name);
}

void NormalMenu::setPicture(QString pictureDir)
{
    imageLabel->setPixmap(QPixmap(pictureDir).scaled(QSize(80,80)));
}

NormalMenu::~NormalMenu()
{

}

 
void NormalMenu::setTestName(QString name)
{
    testName->setText(name);
}
 
void NormalMenu::setPicture(QString pictureDir)
{
    imageLabel->setPixmap(QPixmap(pictureDir).scaled(QSize(80,80)));
}
 
NormalMenu::~NormalMenu()
{
 
}

场景

#ifndef VIEWWIDGET_H
#define VIEWWIDGET_H

#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QTimeLine>

class ViewWidget :  public QGraphicsView
{
    Q_OBJECT

public:
    ViewWidget(QWidget *parent = 0);
    void addTestMenu(QString PixDir,QString TestTitle);
    void CenterPageChange(quint8 page);
    void DisplayModeSet(quint8 Displaymode);
    void initDisplay(qreal initwith,qreal initheigh);
    ~ViewWidget();
protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

private slots:
    void Animatordisplay(int frame);

private:

    QTimeLine m_pageAnimator;
    QGraphicsScene scene;
    QList<QGraphicsProxyWidget*> tab1;
    quint8 DisplayMode;
    bool m_bPressed;
    bool Refresh;
    QPoint m_point;
    qreal m_offset;
//    qreal xposition;
    qreal PosYposition;
    qreal CenterToCenter;
    QRectF CenterGeometry;
    qreal NormalHeight=380;
    qreal NormalWith=230;

    qreal m_translationChangeRate;
    qreal m_scaleChangeRate;
    qreal m_opacityChangeRate;
};

#endif // WIDGET_H

 

#include "ViewWidget.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QScrollBar>
#include <QGraphicsItem>

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>

#include "NormalMenu.h"
#include <qdebug.h>
#define PageMaxNub      5

ViewWidget::ViewWidget(QWidget *parent)
    : QGraphicsView(parent)
{

    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    setWindowFlag(Qt::FramelessWindowHint);
    QGraphicsView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QGraphicsView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_bPressed=false;
    connect(&m_pageAnimator, SIGNAL(frameChanged(int)), SLOT(Animatordisplay(int)));
    m_pageAnimator.setDuration(1000);
    m_pageAnimator.setFrameRange(0, 40);
}


void ViewWidget::addTestMenu(QString PixDir, QString TestTitle)
{
    NormalMenu *my = new NormalMenu();
    QList<QLabel *> text = my->findChildren<QLabel*>();
    text.at(1)->setText(TestTitle);//.append(QString("%1").arg(tab1.count()))
    text.at(0)->setPixmap(QPixmap(PixDir));//"://image/test1.png"
    QGraphicsProxyWidget *pProxy = scene.addWidget(my);
    pProxy->setMinimumSize(100,100);
    tab1<<pProxy;

    //requeue the test
    quint8 initcenterlItem=1;
    initcenterlItem=tab1.length()/2+1;

    CenterPageChange(initcenterlItem);
}

void ViewWidget::DisplayModeSet(quint8 Displaymode)
{
    DisplayMode=Displaymode;
    if(DisplayMode==0)
    {
        CenterToCenter=scene.sceneRect().width()/PageMaxNub-10;
        NormalWith      =scene.sceneRect().width()/PageMaxNub+100;
        NormalHeight    =scene.height()*2/3;
        CenterGeometry.setRect(scene.sceneRect().x()+scene.width()/2-NormalWith/2,scene.sceneRect().y()+scene.height()/2-NormalHeight/2,NormalWith,NormalHeight);
    }
    else
    {
        CenterGeometry.setRect(scene.sceneRect().x()+scene.width()/2-NormalWith/2,scene.sceneRect().y()+4,NormalWith,scene.height()-8);
        NormalHeight=scene.height()-8;
        NormalWith=scene.width()/PageMaxNub;
        CenterToCenter=NormalWith;
    }
    //    NormalWith=;
}

void ViewWidget::initDisplay(qreal initwith,qreal initheigh)
{
    resize(initwith,initheigh);
    setScene(&scene);
    scene.setSceneRect(0,0,initwith,initheigh);
    DisplayModeSet(0);
//    CenterGeometry.setRect(scene.sceneRect().x()+scene.width()/2-NormalWith/2,scene.sceneRect().y()+scene.height()/2-NormalHeight/2,NormalWith,NormalHeight);
    scene.addPixmap(QPixmap("://image/bg.png"));
    addTestMenu("://image/test1.png","a");
    addTestMenu("://image/test1.png","b");
    addTestMenu("://image/test1.png","c");
    addTestMenu("://image/test1.png","d");
    addTestMenu("://image/test1.png","e");
    addTestMenu("://image/test1.png","f");
    addTestMenu("://image/test1.png","g");
    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    scale(1,1);
    setWindowFlag(Qt::FramelessWindowHint);
    QGraphicsView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QGraphicsView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_bPressed=false;
    connect(&m_pageAnimator, SIGNAL(frameChanged(int)), SLOT(Animatordisplay(int)));
    m_pageAnimator.setDuration(1000);
    m_pageAnimator.setFrameRange(0, 40);

}

void ViewWidget::CenterPageChange(quint8 page)
{
    qreal xtemp;
    qreal ytemp;
    for(int i=0;i<tab1.length();i++)
    {
        qreal offx;
        qreal scaleit=1;
//        qreal o=1;
        ytemp=CenterGeometry.y();
        if((i+1)<page)
        {
            offx=(page-(i+1))*CenterToCenter;
            xtemp=CenterGeometry.x()+NormalWith/2-offx-NormalWith/2;
            tab1[i]->setGeometry(QRectF(xtemp,ytemp,NormalWith,NormalHeight));
         }
        else if ((i+1)>page)
        {
            offx=((i+1)-page)*CenterToCenter;
            xtemp=CenterGeometry.x()+NormalWith/2+offx-NormalWith/2;
            tab1[i]->setGeometry(QRectF(xtemp,ytemp,NormalWith,NormalHeight));
        }
        else
        {
            offx=0;
            xtemp=CenterGeometry.x();
            tab1[i]->setGeometry(QRectF(xtemp,ytemp,NormalWith,NormalHeight));
        }
        tab1[i]->setTransformOriginPoint(NormalWith/2,NormalHeight/2);
        scaleit = 1-0.2*(offx/(scene.sceneRect().width()/2));
        if(DisplayMode==1)
        {
            scaleit = 1;
        }
        qreal Zvalue;
        Zvalue=1000-abs(tab1[i]->pos().x()-CenterGeometry.x()+10)/10;
        tab1[i]->setZValue(Zvalue);
        tab1[i]->setScale(scaleit);
        tab1[i]->setOpacity(1-offx/(scene.sceneRect().width()/2));
        qDebug() <<" i" <<i<<" " << scaleit;
    }

}

ViewWidget::~ViewWidget()
{

}
void ViewWidget::mousePressEvent(QMouseEvent *event)
{
    m_bPressed=true;
    Refresh=false;
    m_point = event->pos();
    m_offset=0.0;
    QGraphicsView::mousePressEvent(event);
}

void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPressed=false;
    if (m_pageAnimator.state() != QTimeLine::NotRunning)
    {
        m_pageAnimator.stop();
    }
    quint8 pagechange=0;
    qreal Zvalue=0;
    for(int i=0; i<tab1.length(); i++){
        //get middle pos
        if(Zvalue<tab1[i]->zValue())
        {
            pagechange=i+1;
            Zvalue=tab1[i]->zValue();
        }
    }
    CenterPageChange(pagechange);
    QGraphicsView::mouseReleaseEvent(event);
}

void ViewWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bPressed){
        if(Refresh==true)
        {
            m_point = event->pos();
            Refresh=false;
        }
        m_offset = event->pos().x()-m_point.x();
        if(m_offset>0)
        {
            if(m_offset>28) m_offset=28;
        }
        else if(m_offset<0)
        {
            if(m_offset<-28) m_offset=-28;
        }
        if (m_pageAnimator.state() == QTimeLine::NotRunning)
        {
            m_pageAnimator.start();
        }
    }

    QGraphicsView::mouseMoveEvent(event);
}

void ViewWidget::Animatordisplay(int frame)
{
    qreal tempOffSet=m_offset;
    if(abs(tempOffSet)>=3)
    {
        for(int i=0; i<tab1.length(); i++){
            qreal nowposX;
            qreal nowposY;
            qreal nowCentel;
            qreal nowoffsetToMiddleScene;
            nowCentel=tab1[i]->geometry().x()+tab1[i]->geometry().width()/2+tempOffSet;
            nowoffsetToMiddleScene=abs(nowCentel-(CenterGeometry.x()+NormalWith/2));
            nowposX=(nowCentel-NormalWith/2);
            nowposY=CenterGeometry.y();
            tab1[i]->setGeometry(QRectF(nowposX,nowposY,NormalWith,NormalHeight));
            qreal scaleit = 1-0.2*(nowoffsetToMiddleScene/(scene.sceneRect().width()/2));
            if(DisplayMode==1)
            {
                scaleit = 1;
            }
            tab1[i]->setScale(scaleit);
            qreal Zvalue;
            Zvalue=1000-abs(tab1[i]->pos().x()-CenterGeometry.x()+10)/10;
            tab1[i]->setZValue(Zvalue);
            tab1[i]->setOpacity(1-nowoffsetToMiddleScene/(scene.sceneRect().width()/2));
        }
        m_offset=0.0;
        Refresh=true;
    }
}

 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值