Using Signals and Slots with Touch Events
-
I'm working on an image viewer that allows users to cycle through thumbnails and move them to a larger viewing area. I want to be able to cycle through a series of pixmaps--stored in a list--by dragging left and right on a promoted graphics view (I'm using a .ui file for the interface). What makes this complicated for me is that the functions for generating the pixmaps and the pixmap list are both in my MainWindow class, and I can't move them to the promoted graphics view class because I need them elsewhere in the MainWindow class (that is, for the main viewing area, which is a non-promoted graphics view). I'm just wondering if there's some way to make this work with signals and slots.
I do have some touch functionality in the promoted graphics view now, but it's fairly rusty:
@sliceView::sliceView(QObject *parent)
{
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
}bool sliceView::viewportEvent(QEvent *event)
{
switch(event->type())
{
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QListQTouchEvent::TouchPoint touchPoints = touchEvent->touchPoints();
if(touchPoints.count() == 2)
{
const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.first();
const QTouchEvent::TouchPoint &touchPoint2 = touchPoints.last();
if(touchPoint1.state() == Qt::TouchPointReleased)
{
emit goBack(event);
}
else if(touchPoint2.state() == Qt::TouchPointReleased)
{
emit goForward(event);
}
}
else if(touchPoints.count() == 3)
{
if(touchEvent->touchPointStates() & Qt::TouchPointPressed)
{
emit changeLevel(event);
}
}
else if(touchPoints.count() == 4)
{
if(touchEvent->touchPointStates() & Qt::TouchPointReleased)
{
emit moveToMainViewer(event);
}
}
return true;
}
default:
break;
}
return QGraphicsView::viewportEvent(event);
}@Here's where the signals and slots link to:
@ connect(ui->viewer, SIGNAL(moveToMainViewer(QEvent *)), this, SLOT(displayImage()));
connect(ui->viewer, SIGNAL(goBack(QEvent *)), this, SLOT(backwardSlice()));
connect(ui->viewer, SIGNAL(goForward(QEvent *)), this, SLOT(forwardSlice()));
connect(ui->viewer, SIGNAL(changeLevel(QEvent *)), this, SLOT(changeLevel()));
.
.
.
void MainWindow::displayImage()
{
QPixmap copyItem(newSlice2->pixmap());
sliceCopy = new slice;
sliceCopy->setPixmap(copyItem);
sliceCopy->setPos(-50, 0);
scene->addItem(sliceCopy);
ui->graphicsView->update();
}void MainWindow::backwardSlice()
{
if ( s >= 0 && s < thumbnailPixmapList.size() )
{
QPixmap nextPix = thumbnailPixmapList.at(s-1);
--s;
ui->horizontalSlider->setValue(s);
newSlice2->setPixmap(nextPix);
ui->viewer->update();
}
}void MainWindow::forwardSlice()
{
if ( s >= 0 && s < thumbnailPixmapList.size() )
{
QPixmap nextPix = thumbnailPixmapList.at(s+1);
++s;
ui->horizontalSlider->setValue(s);
newSlice2->setPixmap(nextPix);
ui->viewer->update();
}
}void MainWindow::changeLevel()
{
QPixmap levelItem(QPixmap::fromImage(levelledSlice(0,s,ui->horizontalSlider_2->sliderPosition(),ui->horizontalSlider_3->sliderPosition())));
newSlice2->setPixmap(levelItem);
ui->viewer->update();}@
If I can't do what I plan to do with signals and slots, please let me know if there are any alternatives I could try.
-
May be it will be interesting for you: "Image Gestures Example":http://doc.qt.nokia.com/4.7/gestures-imagegestures.html
-
[quote author="Vass" date="1309298312"]May be it will be interesting for you: "Image Gestures Example":http://doc.qt.nokia.com/4.7/gestures-imagegestures.html[/quote]
That actually seems like a good idea. Unfortunately, I can't seem to get a QSwipeGesture to work in a QGraphicsView. Here's what I have so far:
Class Declaration:
@class sliceView : public QGraphicsView
{
Q_OBJECT
public:
explicit sliceView(QWidget *parent = 0);
bool viewportEvent(QEvent *event);signals:
void moveToMainViewer(QEvent *);
void goBack(QSwipeGesture *);
void goForward(QEvent *);
void changeLevel(QEvent *);protected:
bool event(QEvent *event);private:
bool gestureEvent(QGestureEvent *event);
void swipeTriggered(QSwipeGesture *);};@
.cpp file:
@
#include "sliceview.h"
#include <QtGui>
#include <QTouchEvent>sliceView::sliceView(QWidget *parent)
: QGraphicsView(parent)
{
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
grabGesture(Qt::SwipeGesture);
}bool sliceView::event(QEvent event)
{
if(event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent>(event));
return QGraphicsView::event(event);
}bool sliceView::gestureEvent(QGestureEvent *event)
{
if(QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(static_cast<QSwipeGesture *> (swipe));
return true;
}void sliceView::swipeTriggered(QSwipeGesture *gesture)
{
if(gesture->state() == Qt::GestureFinished)
{
if(gesture->horizontalDirection() == QSwipeGesture::Left)
emit goBack(gesture);
update();
}
}bool sliceView::viewportEvent(QEvent *event)
{
switch(event->type())
{
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QListQTouchEvent::TouchPoint touchPoints = touchEvent->touchPoints();
if(touchPoints.count() == 3)
{
if(touchEvent->touchPointStates() & Qt::TouchPointPressed)
{
emit changeLevel(event);
}
}
else if(touchPoints.count() == 4)
{
if(touchEvent->touchPointStates() & Qt::TouchPointReleased)
{
emit moveToMainViewer(event);
}
}
return true;
}
default:
break;
}
return QGraphicsView::viewportEvent(event);
}
@