您的位置:首页 > 编程语言 > Qt开发

QT实现屏幕滑动效果窗体

2012-06-04 15:52 633 查看


QT实现屏幕滑动效果窗体

分类: 2010年2010-09-09
22:51 1301人阅读 评论(2) 收藏 举报

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: