QT实现屏幕滑动效果窗体

class CScrollScreenView : public QGraphicsView

{
    Q_OBJECT
public:
    CScrollScreenView(QGraphicsScene *pScene, QWidget *parent);
    //设置试图的布局
    virtual void SetLayout(QGraphicsLayout *pLayout);
    //设置滚动完成的时间,单位:ms
    void SetAnimationDuration(int msecs)
    {
        m_AnimationDuration = msecs;
    }
    //设置滚动的距离,单位:像素
    void SetScrollDist(int dist)
    {
        m_ScrollDist = dist;
    }
    //在父窗体上按场景位置显示试图
    virtual void ShowView()
    {
        this->show();
    }
private:
    int m_AnimationDuration;    //屏幕滚动动画控件
    int m_ScrollDist;           //屏幕滚动的距离
protected:
    void resizeEvent(QResizeEvent *event)
    {
        QGraphicsView::resizeEvent(event);
        fitInView(sceneRect(), Qt::KeepAspectRatio);
    }
    //功能:滚动m_pForm指向的视图窗口
    //参数:LeftScroll为true则是向左滚动,否则是向右滚动;dist是滚动的距离
    virtual void AnimationForm(bool LeftScroll, int dist);
    QWidget *m_pParent;
    bool m_f_PressMouse;
    QPoint m_StartDragPos;
    QPoint m_ScrollStartPos;
    QGraphicsWidget *m_pForm;
    QGraphicsScene *m_pScene;
protected slots:
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
};


CScrollScreenView::CScrollScreenView(QGraphicsScene *pScene, QWidget *parent)
    :QGraphicsView(pScene, parent)
{
    m_pScene = pScene;
    m_pForm = new QGraphicsWidget();
    m_pParent = parent;
    m_f_PressMouse = false;
    m_ScrollStartPos = QPoint(0, 0);
    m_AnimationDuration = 1000;
    m_ScrollDist = 100;
    setBackgroundBrush(QBrush(QColor(0,0,0)));
}
void CScrollScreenView::SetLayout(QGraphicsLayout *pLayout)
{
    m_pForm->setLayout(pLayout);
    m_pScene->addItem(m_pForm);
    pLayout->updateGeometry();
    qreal left = 0, top = 0, right = 0, bottom = 0;
    m_pForm->getContentsMargins(&left, &top, &right, &bottom );
    printf("l=%f,t=%f,r=%f,b=%f/n", left,top,right,bottom);
    printf("w=%f,h=%f/n", m_pForm->size().width(),m_pForm->size().height());
}
void CScrollScreenView::AnimationForm(bool LeftScroll, int dist)
{
    if(!LeftScroll)
    {
        QPropertyAnimation *animation = new QPropertyAnimation(m_pForm, "pos");
        animation->setDuration(m_AnimationDuration);
        animation->setStartValue(m_ScrollStartPos);
        m_ScrollStartPos.setX(m_ScrollStartPos.x() + dist);
        animation->setEndValue(m_ScrollStartPos);
        animation->setEasingCurve(QEasingCurve::Linear);
        animation->start();
    }
    else if(LeftScroll)
    {
        QPropertyAnimation *animation = new QPropertyAnimation(m_pForm, "pos");
        animation->setDuration(m_AnimationDuration);
        animation->setStartValue(m_ScrollStartPos);
        m_ScrollStartPos.setX(m_ScrollStartPos.x() - dist);
        animation->setEndValue(m_ScrollStartPos);
        animation->setEasingCurve(QEasingCurve::Linear);
        animation->start();
    }
}
void CScrollScreenView::mousePressEvent(QMouseEvent *event)
{
    QGraphicsView::mousePressEvent(event);
    m_f_PressMouse = true;
    m_StartDragPos = event->pos();
}
void CScrollScreenView::mouseReleaseEvent(QMouseEvent *event)
{
    QGraphicsView::mouseReleaseEvent(event);
    if(m_f_PressMouse)
    {
        QPoint end_pos = event->pos();
        if(qAbs(end_pos.x() - m_StartDragPos.x()) >= 10)
        {
            if(end_pos.x() < m_StartDragPos.x())
            {
                AnimationForm(true, m_ScrollDist);
            }
            else if(end_pos.x() > m_StartDragPos.x())
            {
                AnimationForm(false, m_ScrollDist);
            }
        }
    }
    m_f_PressMouse = false;
}

你可能感兴趣的:(2010年)