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
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