PDA

View Full Version : QGraphicsView problem



sourena
1st August 2012, 17:34
hi all
I am new to Qt and this is my first post here...
I have 2 question to ask :
1.what does boundingrect method in Qgraphicsitem exactly do?
Qt 4 doc says "QGraphicsView uses this to determine whether the item requires redrawing" so we need it to redraw the item.
is there any way not using boundingbox and redraw items?( just implement it like "return QRectF()" )?
2.below is my test code ...why WheelEvent doesnt work???


test_item.h
#ifndef TEST_ITEM_H
#define TEST_ITEM_H
#include <QGraphicsItem>
class TestItem:public QGraphicsItem
{
public:
TestItem();
private:
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect() const;
};
#endif // TEST_ITEM_H



test_view.h
#ifndef TEST_VIEW_H
#define TEST_VIEW_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QWheelEvent>
#include"test_item.h"
class TestView:public QGraphicsView
{
public:
TestView(QWidget* parent=0);
private:
QGraphicsScene* scene;
TestItem* item;
protected:
void wheelEvent(QWheelEvent *event);
};
#endif // TEST_VIEW_H



test_item.cpp
#include "test_item.h"
#include <QPainter>
TestItem::TestItem():QGraphicsItem(){}
void TestItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawEllipse(200,200,100,100);
}
QRectF TestItem::boundingRect() const
{
return QRectF(0,0,300,300);
}



test_view.cpp
#include"test_view.h"
#include<QGraphicsEllipseItem>
#include <QMessageBox>
TestView::TestView(QWidget *parent):QGraphicsView(parent)
{
scene=new QGraphicsScene(this),
scene->setSceneRect(0,0,640,480);
this->setScene(scene);

item=new TestItem();
scene->addItem(item);
}
void TestView::wheelEvent(QWheelEvent *event)
{
int m_scale=1.8;
if (event->delta() >0)
scale(m_scale,m_scale);
else
scale(1.0/m_scale,1.0/m_scale);
}


main_test.cpp
#include <QApplication>
#include "test_view.h"
int main(int argc,char ** argv)
{
QApplication app(argc,argv);
TestView *v=new TestView();
v->show();
app.exec();
return 0;
}


thanks in advance

ChrisW67
3rd August 2012, 00:11
The bounding rectangle of a graphic item is used to determine whether this item potentially overlaps others to optimise drawing or to detect collisions. If the bounding boxes intersect then the actual graphics items may overlap and redrawing one may affect the other. If the bounding boxes do not overlap then the items are definitely independent. The rectangles are far faster to test than checking the actual, arbitrary shapes would be and can eliminate items rapidly from consideration; only doing expensive checks if required.

Your wheel event always sets the scale to a constant value.

sourena
5th August 2012, 05:45
thank you for reply chris
I fixed the scale problem using
m_scale+=1.8; (just need a plus before equal sign) :)
and using exact bounding box saving a lot of memory(realy lot) an speed. ;)