#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsTextItem>
#include <QMouseEvent>
#include <QScrollBar>
#include <QLabel>
#include <math.h>
#include <QtGui>
{
public:
QLabel *coordinateDisplayLabel;
protected:
void scaleView(qreal scaleFactor);
private:
bool scrolling;
};
Qt::NoButton,Qt::NoButton,Qt::NoModifier)
{
setAttribute (Qt::WA_SetCursor);
setCursor(Qt::CrossCursor);
scale(1,-1);
coordinateDisplayLabel
= new QLabel(0);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
scrolling = false;
}
{
scaleView(pow((double)2, event->delta() / 240.0));
}
void MapView::scaleView(qreal scaleFactor)
{
qreal factor
= matrix
().
scale(scaleFactor, scaleFactor
).
mapRect(QRectF(0,
0,
1,
1)).
width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
{
lastMouseEvent
= QMouseEvent(event
->type
(), event
->pos
(), event
->globalPos
(),
event->button(),event->buttons(),event->modifiers());
}
{
if(event->button() == Qt::MidButton)
{
setCursor(Qt::ClosedHandCursor);
scrolling = true;
storeMouseEvent(event);
event->accept();
return;
}
}
{
//QGraphicsScene::mousePressEvent(mouseEvent);
qDebug() << mouseEvent->scenePos();
}
{
if(scrolling)
{
QPoint delta
= event
->globalPos
() - lastMouseEvent.
globalPos();
hBar->setValue(hBar->value() - delta.x());
vBar->setValue(vBar->value() - delta.y());
storeMouseEvent(event);
event->accept();
return;
}
sceneCoordinates = mapFromScene(event->pos());
cursorXString.setNum(sceneCoordinates.x());
cursorYString.setNum(sceneCoordinates.y());
coordinateDisplayLabel->setText("x = " + cursorXString + " " + "y = " + cursorYString);
}
{
if((event->button() == Qt::MidButton) && (scrolling == true))
{
setCursor(Qt::CrossCursor);
scrolling = false;
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
}
int main( int argc, char **argv )
{
MapView *view = new MapView;
scene->setSceneRect(-30000, -30000, 60000, 60000);
scene->setBackgroundBrush(Qt::black);
scene
->addLine
( -50,
0,
50,
0,
(QPen(Qt
::white,
0)));
scene
->addLine
( 0,
-50,
0,
50,
(QPen(Qt
::white,
0)));
item1.setDefaultTextColor(Qt::white);
item1.setPos(0,0);
item1.scale(1,-1);
scene->addItem(&item1);
scene
->addLine
( 200,
250,
300,
250,
(QPen(Qt
::white,
0)));
scene
->addLine
( 250,
300,
250,
200,
(QPen(Qt
::white,
0)));
item2.setDefaultTextColor(Qt::white);
item2.setPos(250,250);
item2.scale(1,-1);
scene->addItem(&item2);
view->setScene(scene);
mw->setCentralWidget(view);
statusbar->addPermanentWidget(view->coordinateDisplayLabel, 0);
mw->setStatusBar(statusbar);
mw->show();
return a.exec();
}
#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsTextItem>
#include <QMouseEvent>
#include <QScrollBar>
#include <QLabel>
#include <math.h>
#include <QtGui>
class MapView : public QGraphicsView
{
public:
MapView(QWidget *parent = 0);
QPoint sceneCoordinates;
QLabel *coordinateDisplayLabel;
protected:
void scaleView(qreal scaleFactor);
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
private:
QString cursorXString;
QString cursorYString;
void storeMouseEvent(QMouseEvent *event);
bool scrolling;
QMouseEvent lastMouseEvent;
};
MapView::MapView(QWidget *parent)
: QGraphicsView(parent), lastMouseEvent(QEvent::None,QPoint(),
Qt::NoButton,Qt::NoButton,Qt::NoModifier)
{
QGraphicsScene scene;
setAttribute (Qt::WA_SetCursor);
setCursor(Qt::CrossCursor);
scale(1,-1);
coordinateDisplayLabel = new QLabel(0);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
scrolling = false;
}
void MapView::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, event->delta() / 240.0));
}
void MapView::scaleView(qreal scaleFactor)
{
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
void MapView::storeMouseEvent(QMouseEvent *event)
{
lastMouseEvent = QMouseEvent(event->type(), event->pos(), event->globalPos(),
event->button(),event->buttons(),event->modifiers());
}
void MapView::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::MidButton)
{
setCursor(Qt::ClosedHandCursor);
scrolling = true;
storeMouseEvent(event);
event->accept();
return;
}
}
void MapView::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//QGraphicsScene::mousePressEvent(mouseEvent);
qDebug() << mouseEvent->scenePos();
}
void MapView::mouseMoveEvent(QMouseEvent *event)
{
if(scrolling)
{
QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
hBar->setValue(hBar->value() - delta.x());
vBar->setValue(vBar->value() - delta.y());
storeMouseEvent(event);
event->accept();
return;
}
sceneCoordinates = mapFromScene(event->pos());
cursorXString.setNum(sceneCoordinates.x());
cursorYString.setNum(sceneCoordinates.y());
coordinateDisplayLabel->setText("x = " + cursorXString + " " + "y = " + cursorYString);
}
void MapView::mouseReleaseEvent(QMouseEvent *event)
{
if((event->button() == Qt::MidButton) && (scrolling == true))
{
setCursor(Qt::CrossCursor);
scrolling = false;
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QMainWindow *mw = new QMainWindow();
MapView *view = new MapView;
QStatusBar *statusbar = new QStatusBar;
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(-30000, -30000, 60000, 60000);
scene->setBackgroundBrush(Qt::black);
scene->addLine ( -50, 0, 50, 0, (QPen(Qt::white, 0)));
scene->addLine ( 0, -50, 0, 50, (QPen(Qt::white, 0)));
QGraphicsTextItem item1("0,0");
item1.setDefaultTextColor(Qt::white);
item1.setPos(0,0);
item1.scale(1,-1);
scene->addItem(&item1);
scene->addLine ( 200, 250, 300, 250, (QPen(Qt::white, 0)));
scene->addLine ( 250, 300, 250, 200, (QPen(Qt::white, 0)));
QGraphicsTextItem item2("250,250");
item2.setDefaultTextColor(Qt::white);
item2.setPos(250,250);
item2.scale(1,-1);
scene->addItem(&item2);
view->setScene(scene);
mw->setCentralWidget(view);
statusbar->addPermanentWidget(view->coordinateDisplayLabel, 0);
mw->setStatusBar(statusbar);
mw->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks