i,
I have implemented a swipe class to implement swiping on a widget. However the resulting scroll is jerky and not smooth. Sometimes while swiping up/down the widget would scroll in the direction opposite to what it should.
The code is as follows:
SwipeWidget.
===========
#ifndef SWIPEWIDGET_H
#define SWIPEWIDGET_H
#include <QWidget>
#include <QGesture>
#include <QGestureEvent>
#include <QScrollBar>
/*#include "flickable.h"
class SwipeWidgetDlg : public QWidget, public Flickable
{
Q_OBJECT
public:
explicit SwipeWidgetDlg(QWidget *parent = 0);
void setInitialPosition(int topPos, int botPos);
void setWidgetHeight(int widgetHeight);
signals:
void widgetClicked(QWidget *);
protected:
// reimplement for SwipeWidgetDlg
virtual QPoint scrollOffset() const;
// reimplement for SwipeWidgetDlg
virtual bool setScrollOffset(const QPoint &offset);
protected:
void keyReleaseEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
int iTy;
int iBy;
int m_initTopPos;
int m_initBotPos;
int m_offset;
int m_height;
int m_highlight;
int m_selected;
QPoint m_mouseDown;
};*/
class SwipeWidgetDlg : public QWidget
{
Q_OBJECT
private:
QPoint m_mouseDown;
QPoint m_mouseMove;
int iTy;
int iBy;
int m_initTopPos;
int m_initBotPos;
int m_totalHeight;
int m_height;
bool m_clickStatus;
int iM;
public:
explicit SwipeWidgetDlg(QWidget *parent = 0);
void setInitialPosition(int topPos, int botPos);
void setWidgetHeight(int widgetHeight);
void setTotalHeight(int totalHeight);
void mousePressEvent (QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseMoveEvent (QMouseEvent *);
void setClickStatus(bool aClickStatus);
void setMultFact(int m)
{
iM = m;
}
protected:
bool eventFilter(QObject *obj, QEvent *event);
signals:
void widgetClicked(QWidget *);
};
#endif // SWIPEWIDGET_H
SwipWidget.cpp
=============
#include "SwipeWidget.h"
#include <QGestureEvent>
#include <QGesture>
#include <QLabel>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QHBoxLayout>
#include <QApplication>
#include <QtGlobal>
#include <QSettings>
#include <QPainter>
#include <QTimer>
/*SwipeWidgetDlg::SwipeWidgetDlg(QWidget *parent)
: QWidget(parent) {
iTy = 0;
iBy = 0;
m_height = 0;
m_offset = 0;
m_highlight = -1;
m_selected = -1;
setMouseTracking(true);
Flickable::setAcceptMouseClick(this);
}
void SwipeWidgetDlg::keyReleaseEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Down) {
qWarning("KEY DOWN");
m_offset += 20;
event->accept();
update();
return;
}
if (event->key() == Qt::Key_Up) {
qWarning("KEY UP");
m_offset -= 20;
event->accept();
update();
return;
}
}
void SwipeWidgetDlg::mousePressEvent(QMouseEvent *event) {
qWarning("In SwipeWidgetDlg::mousePressEvent");
Flickable::handleMousePress(event);
if (event->isAccepted())
return;
if (event->button() == Qt::LeftButton) {
m_mouseDown = event->pos();
int y = event->pos().y() + m_offset;
int i = y / m_height;
if (i != m_highlight) {
m_highlight = i;
m_selected = -1;
update();
}
event->accept();
}
}
void SwipeWidgetDlg::mouseMoveEvent(QMouseEvent *event) {
Flickable::handleMouseMove(event);
}
void SwipeWidgetDlg::mouseReleaseEvent(QMouseEvent *event) {
int delta = m_mouseDown.y() - event->pos().y();
qWarning("In mouserelease: delta = %d", delta);
if (abs(delta) < 10)
{
int X = event->x();
int Y = event->y();
QWidget *elem = childAt(X,Y);
if(elem)
{
iTy = m_initTopPos;
iBy = m_initBotPos;
emit widgetClicked(elem);
}
}
else
{
Flickable::handleMouseRelease(event);
if (event->isAccepted())
return;
if (event->button() == Qt::LeftButton) {
m_selected = m_highlight;
event->accept();
update();
}
}
}
QPoint SwipeWidgetDlg::scrollOffset() const {
return QPoint(0, m_offset);
}
bool SwipeWidgetDlg::setScrollOffset(const QPoint &offset) {
int yy = offset.y();
qWarning("In SwipeWidgetDlg::setScrollOffset");
if (iTy + yy >= 0 && iBy + yy <= geometry().height()) {
iTy += yy;
iBy += yy;
qWarning("Scroll by %d", -yy);
scroll(0, -yy);
return true;
}
return false;
}*/
SwipeWidgetDlg::SwipeWidgetDlg(QWidget *parent) :
QWidget(parent)
{
iTy = 0;
iBy = 0;
m_initTopPos = 0;
m_initBotPos = 0;
m_height = 0;
m_totalHeight = 0;
m_mouseDown = QPoint(0, 0);
m_mouseMove = QPoint(0, 0);
m_clickStatus = true;
iM = 2;
}
bool SwipeWidgetDlg::eventFilter(QObject *obj, QEvent *event)
{
QMouseEvent *e;
if (event->type() == QEvent::MouseMove)
{
e = static_cast<QMouseEvent*>(event);
this->mouseMoveEvent(e);
}
else if(event->type() == QEvent::MouseButtonPress)
{
e = static_cast<QMouseEvent*>(event);
this->mousePressEvent(e);
}
else if(event->type() == QEvent::MouseButtonRelease)
{
e = static_cast<QMouseEvent*>(event);
this->mouseReleaseEvent(e);
}
return QObject::eventFilter(obj, event);
}
void SwipeWidgetDlg::mousePressEvent(QMouseEvent *e)
{
qWarning("Mouse Press Event\n");
m_mouseDown = e->pos();
m_mouseMove = e->pos();
}
void SwipeWidgetDlg::mouseMoveEvent (QMouseEvent *e)
{
QPointF mouseMove = e->pos();
int delta = (m_mouseMove.y() - mouseMove.y())*iM;
qWarning("Mouse Move(%d, %d): iTy = %d, iBy = %d, delta = %d, total height = %d", e->pos().x(), e->pos().y(), iTy, iBy, delta, m_totalHeight);
if (iTy + delta >= 0 && iBy + delta <= m_totalHeight)
{
m_mouseMove.setX(mouseMove.x());
m_mouseMove.setY(mouseMove.y());
qWarning("About scroll\n");
scroll(0, -delta);
iTy += delta;
iBy += delta;
}
}
void SwipeWidgetDlg::mouseReleaseEvent(QMouseEvent *e)
{
qWarning("Mouse Release Event\n");
if(m_clickStatus)
{
int delta = m_mouseDown.y() - e->pos().y();
if (abs(delta) < 10)
{
int X = e->x();
int Y = e->y();
QWidget *elem = childAt(X,Y);
if(elem)
{
//iTy = m_initTopPos;
//iBy = m_initBotPos;
emit widgetClicked(elem);
}
}
}
}
void SwipeWidgetDlg::setInitialPosition(int topPos, int botPos)
{
iTy = topPos;
m_initTopPos = topPos;
iBy = botPos;
m_initBotPos = botPos;
}
void SwipeWidgetDlg::setWidgetHeight(int widgetHeight)
{
qWarning("setWidgetHeight: %d\n", widgetHeight);
m_height = widgetHeight;
}
void SwipeWidgetDlg::setTotalHeight(int totalHeight)
{
qWarning("setTotalHeight: %d\n", totalHeight);
m_totalHeight = totalHeight;
}
void SwipeWidgetDlg::setClickStatus(bool aClickStatus)
{
m_clickStatus = aClickStatus;
}
I have installed the SwipeWidgetDlg object "widget" as filter for all the controls that use mouse events (buttons, etc). so that the SwipeWidgetDlg gets the mouse events.
Kindly help as to what is wrong with code and what should I do?
thanx and regards
Zia


... after you fix it try playing with QTimeLine animation to add some cool kinetic effect


