mcarter
1st April 2012, 22:58
I have created the following small program that demonstrates the problem that I am seeing in a much larger application.
I am setting a oblong rectangle for a qgraphicsview to represent that data that I am expecting, but not sure if that has anything to do with it. I also have an item that is rather small when compared to the overall rectangle size. So with this example, you need to zoom in (wheel) quite a bit to get to a reasonable view of the small rectangle. Once the item is viewable you will notice that the cursor will change when it "enters" the item. However, the edge from the top of the box where the view/scene thinks the item starts is not at the edge of the item but several pixels away (higher). And gets worse as you zoom in.
I am currently building this with 4.7.2 under Linux and do not have access to 4.8. Strange thing, if I build with 4.6.3, everything looks right. The edge of the item is actually at pixel edge of the item.
#ifndef _MAIN_H_
#define _MAIN_H_
#include <QGraphicsView>
class View : public QGraphicsView
{
Q_OBJECT;
public:
View( QWidget* parent = 0 );
~View();
protected:
void wheelEvent( QWheelEvent* event );
};
#endif
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QWheelEvent>
#include <QDebug>
#include <cmath>
#include "main.h"
/*----------------------------------------------------------------------------*/
View::View( QWidget* parent )
: QGraphicsView( parent )
{
setFrameStyle( QFrame::NoFrame );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
}
View::~View()
{
}
void View::wheelEvent( QWheelEvent* event )
{
double numDegrees = event->delta() / 8.0;
double numSteps = numDegrees / 15.0;
double factor = std::pow(1.125, numSteps);
scale(factor, factor);
}
/*----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow * pWin = new QMainWindow;
double dXMin = 2000000;
double dXWidth = 28000000;
double dYMin = 0.0;
double dYHeight = 86400.;
QGraphicsScene * pScene = new QGraphicsScene( dXMin, dYMin,
dXWidth, dYHeight );
View * pView = new View;
pView->setScene(pScene);
pView->fitInView( pScene->sceneRect() );
pWin->setCentralWidget(pView);
QRectF itemRect( 0., 0., 4000., 5. );
QGraphicsRectItem *pItem = new QGraphicsRectItem( itemRect );
pItem->setPos( pView->sceneRect().center() );
pItem->setCursor(QCursor(Qt::SizeAllCursor));
pScene->addItem( pItem );
pWin->show();
return app.exec();
}
I am setting a oblong rectangle for a qgraphicsview to represent that data that I am expecting, but not sure if that has anything to do with it. I also have an item that is rather small when compared to the overall rectangle size. So with this example, you need to zoom in (wheel) quite a bit to get to a reasonable view of the small rectangle. Once the item is viewable you will notice that the cursor will change when it "enters" the item. However, the edge from the top of the box where the view/scene thinks the item starts is not at the edge of the item but several pixels away (higher). And gets worse as you zoom in.
I am currently building this with 4.7.2 under Linux and do not have access to 4.8. Strange thing, if I build with 4.6.3, everything looks right. The edge of the item is actually at pixel edge of the item.
#ifndef _MAIN_H_
#define _MAIN_H_
#include <QGraphicsView>
class View : public QGraphicsView
{
Q_OBJECT;
public:
View( QWidget* parent = 0 );
~View();
protected:
void wheelEvent( QWheelEvent* event );
};
#endif
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QWheelEvent>
#include <QDebug>
#include <cmath>
#include "main.h"
/*----------------------------------------------------------------------------*/
View::View( QWidget* parent )
: QGraphicsView( parent )
{
setFrameStyle( QFrame::NoFrame );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
}
View::~View()
{
}
void View::wheelEvent( QWheelEvent* event )
{
double numDegrees = event->delta() / 8.0;
double numSteps = numDegrees / 15.0;
double factor = std::pow(1.125, numSteps);
scale(factor, factor);
}
/*----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow * pWin = new QMainWindow;
double dXMin = 2000000;
double dXWidth = 28000000;
double dYMin = 0.0;
double dYHeight = 86400.;
QGraphicsScene * pScene = new QGraphicsScene( dXMin, dYMin,
dXWidth, dYHeight );
View * pView = new View;
pView->setScene(pScene);
pView->fitInView( pScene->sceneRect() );
pWin->setCentralWidget(pView);
QRectF itemRect( 0., 0., 4000., 5. );
QGraphicsRectItem *pItem = new QGraphicsRectItem( itemRect );
pItem->setPos( pView->sceneRect().center() );
pItem->setCursor(QCursor(Qt::SizeAllCursor));
pScene->addItem( pItem );
pWin->show();
return app.exec();
}