Results 1 to 3 of 3

Thread: QGraphicsView problem

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView problem

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

    Qt Code:
    1. test_item.h
    2. #ifndef TEST_ITEM_H
    3. #define TEST_ITEM_H
    4. #include <QGraphicsItem>
    5. class TestItem:public QGraphicsItem
    6. {
    7. public:
    8. TestItem();
    9. private:
    10. protected:
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    12. QRectF boundingRect() const;
    13. };
    14. #endif // TEST_ITEM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. test_view.h
    2. #ifndef TEST_VIEW_H
    3. #define TEST_VIEW_H
    4. #include <QGraphicsView>
    5. #include <QGraphicsScene>
    6. #include <QWheelEvent>
    7. #include"test_item.h"
    8. class TestView:public QGraphicsView
    9. {
    10. public:
    11. TestView(QWidget* parent=0);
    12. private:
    13. TestItem* item;
    14. protected:
    15. void wheelEvent(QWheelEvent *event);
    16. };
    17. #endif // TEST_VIEW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. test_item.cpp
    2. #include "test_item.h"
    3. #include <QPainter>
    4. TestItem::TestItem():QGraphicsItem(){}
    5. void TestItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    6. {
    7. painter->drawEllipse(200,200,100,100);
    8. }
    9. QRectF TestItem::boundingRect() const
    10. {
    11. return QRectF(0,0,300,300);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. test_view.cpp
    2. #include"test_view.h"
    3. #include<QGraphicsEllipseItem>
    4. #include <QMessageBox>
    5. TestView::TestView(QWidget *parent):QGraphicsView(parent)
    6. {
    7. scene=new QGraphicsScene(this),
    8. scene->setSceneRect(0,0,640,480);
    9. this->setScene(scene);
    10.  
    11. item=new TestItem();
    12. scene->addItem(item);
    13. }
    14. void TestView::wheelEvent(QWheelEvent *event)
    15. {
    16. int m_scale=1.8;
    17. if (event->delta() >0)
    18. scale(m_scale,m_scale);
    19. else
    20. scale(1.0/m_scale,1.0/m_scale);
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. main_test.cpp
    2. #include <QApplication>
    3. #include "test_view.h"
    4. int main(int argc,char ** argv)
    5. {
    6. QApplication app(argc,argv);
    7. TestView *v=new TestView();
    8. v->show();
    9. app.exec();
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsView problem

    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.

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView problem

    thank you for reply chris
    I fixed the scale problem using
    Qt Code:
    1. m_scale+=1.8;
    To copy to clipboard, switch view to plain text mode 
    (just need a plus before equal sign)
    and using exact bounding box saving a lot of memory(realy lot) an speed.

Similar Threads

  1. problem using qgraphicsview
    By sandhyarani in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2009, 13:30
  2. QGraphicsView Problem
    By Toaders in forum Newbie
    Replies: 1
    Last Post: 15th May 2009, 18:48
  3. QGraphicsView Problem
    By hakkman in forum Qt Programming
    Replies: 2
    Last Post: 14th June 2008, 20:27
  4. QGraphicsView Problem !!
    By Gamalof in forum Qt Programming
    Replies: 3
    Last Post: 14th June 2008, 13:55
  5. Problem with QGraphicsView.
    By kiranraj in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.