Results 1 to 2 of 2

Thread: Rendering items to graphicsscene

  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Rendering items to graphicsscene

    hi,

    i want to add number of ballons(GraphicsItem) into graphicsScene depending upon the position in main.cpp given.
    main.cpp
    Qt Code:
    1. #include<QtGui>
    2. #include"ballon.h"
    3.  
    4. int main(int argc,char **argv)
    5. {
    6. QApplication app(argc,argv);
    7. scene.setSceneRect(0,0,300,300);
    8. QGraphicsRectItem *rectItem=new QGraphicsRectItem(0,0,300,30);
    9. scene.addItem(rectItem);
    10. QGraphicsTextItem *textItem=scene.addText("After / Before",QFont("",18,QFont::Bold));
    11. scene.setBackgroundBrush(Qt::yellow);
    12. scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    13. for(int i=0;i<5;i++)
    14. {
    15. Ballon *ball=new Ballon("H");
    16. ball->setPos(200,200);
    17. scene.addPath(ball->shape());
    18. }
    19. QGraphicsView view(&scene);
    20. view.setRenderHint(QPainter::Antialiasing);
    21. view.setCacheMode(QGraphicsView::CacheBackground);
    22. view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    23. view.show();
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    ballon.cpp
    Qt Code:
    1. #include "ballon.h"
    2. #include<QFont>
    3.  
    4. Ballon::Ballon(QString text): _mtext(text)
    5. {
    6. setPos(270,270);
    7. }
    8. void Ballon::setText(QString text)
    9. {
    10. _mtext=text;
    11. }
    12. QString Ballon::getText()
    13. {
    14. return _mtext;
    15. }
    16. void Ballon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    17. {
    18.  
    19. }
    20. QPainterPath Ballon::shape() const
    21. {
    22. path.addEllipse(boundingRect());
    23. return path;
    24. }
    25. QRectF Ballon::boundingRect() const
    26. {
    27. return QRectF(40,40,30,30);
    28. }
    To copy to clipboard, switch view to plain text mode 

    ellipse is drawing at boundingRect dimensions but not at ball.setpos(200,200)
    i went thru examples but i dont find where the mistake lies.
    please help me

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rendering items to graphicsscene

    QGraphicsPathItem * QGraphicsScene::addPath ( const QPainterPath & path, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

    Creates and adds a path item to the scene, and returns the item pointer. The geometry of the path is defined by path, and its pen and brush are initialized to pen and brush.

    Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).
    you have to set pos after add to scene
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Rendering order of items in a QwtPlot
    By citocran in forum Qwt
    Replies: 1
    Last Post: 7th January 2011, 15:47
  2. disabling multiple selection of items in graphicsscene/view
    By lightning2911 in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2010, 08:06
  3. QGridLayout: how to know it has finished rendering items?
    By slash_blog in forum Qt Programming
    Replies: 0
    Last Post: 8th June 2010, 12:06
  4. Replies: 6
    Last Post: 27th March 2010, 05:42
  5. Rendering items on QListView
    By rcintra in forum Qt Programming
    Replies: 6
    Last Post: 10th May 2007, 22:22

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.