#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QScrollBar>
{
public:
protected:
private:
bool scrolling;
};
lastMouseEvent
(QEvent::None,
QPoint(),Qt
::NoButton,Qt
::NoButton,Qt
::NoModifier){
// If at least one of these has the policy ..AlwaysOn, scrolling works
// (in both directions)!
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
{
lastMouseEvent
= QMouseEvent(event
->type
(), event
->pos
(),
event->globalPos(),
event->button(),event->buttons(),event->modifiers());
}
{
// just handle left button presses on the background
if(!item) {
if((event->button() == Qt::LeftButton)) {
scrolling = true;
storeMouseEvent(event);
event->accept();
return;
}
}
event->ignore();
}
{
// perform scrolling if scrolling is active
if(scrolling) {
QPoint delta
= event
->globalPos
() - lastMouseEvent.
globalPos();
hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
}
{
// end scrolling mode if it was active
if((event->button() == Qt::LeftButton) && (scrolling == true)) {
scrolling = false;
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
}
int main(int argc, char *argv[])
{
MyWidget widget(&scene);
scene.addItem(&item);
widget.show();
scene.setSceneRect(-1000,-1000,2000,2000);
return app.exec();
}
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QScrollBar>
class MyWidget : public QGraphicsView
{
public:
MyWidget(QGraphicsScene *scene, QWidget *parent = 0);
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
private:
void storeMouseEvent(QMouseEvent *event);
bool scrolling;
QMouseEvent lastMouseEvent;
};
MyWidget::MyWidget(QGraphicsScene *scene, QWidget *parent)
: QGraphicsView(scene, parent), scrolling(false),
lastMouseEvent(QEvent::None,QPoint(),Qt::NoButton,Qt::NoButton,Qt::NoModifier)
{
// If at least one of these has the policy ..AlwaysOn, scrolling works
// (in both directions)!
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void MyWidget::storeMouseEvent(QMouseEvent *event)
{
lastMouseEvent = QMouseEvent(event->type(), event->pos(),
event->globalPos(),
event->button(),event->buttons(),event->modifiers());
}
void MyWidget::mousePressEvent(QMouseEvent *event)
{
// just handle left button presses on the background
QGraphicsItem *item = itemAt(event->pos());
if(!item) {
if((event->button() == Qt::LeftButton)) {
scrolling = true;
storeMouseEvent(event);
event->accept();
return;
}
}
event->ignore();
QGraphicsView::mousePressEvent(event);
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
// perform scrolling if scrolling is active
if(scrolling) {
QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
QGraphicsView::mouseMoveEvent(event);
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
// end scrolling mode if it was active
if((event->button() == Qt::LeftButton) && (scrolling == true)) {
scrolling = false;
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
QGraphicsView::mouseReleaseEvent(event);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
MyWidget widget(&scene);
QGraphicsTextItem item("hello");
scene.addItem(&item);
widget.show();
scene.setSceneRect(-1000,-1000,2000,2000);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks