PDA

View Full Version : qgraphicsrectitem "boundary" issue



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();
}

wysota
1st April 2012, 23:30
Does the effect change if you scale all your values by the factor of say... 0.01 (so that itemRect becomes 0,0, 40, 0.05)?

mcarter
2nd April 2012, 02:48
adjusting the rect item value by 0.01 (0,0,40,0.05) makes the edge problem worse . . . but again. 4.6.3 works fine

mcarter
2nd April 2012, 15:48
examining the Qt bugs, it looks like someone has finally fixed this in the latest Qt 4.8.1 release . . . fixed itemsAtPosition method in qgraphicsscene.cpp . . . using some of the 4.6 code. Currently not able to move to 4.8 base, so will stick with 4.6 for now.