Results 1 to 7 of 7

Thread: add points/path to QGraphicsView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: add points/path to QGraphicsView

    Hi!

    You should probably not set the boundingRect directly, but calculate it by how much space your diagram requires. But of course you can also do it the other way around and set the boundingRect from the outside and adapt the scale of your diagram accordingly. Depends on what you want to achieve.

    addItem doesn't call paint. It just adds the item to an internal list of graphic items of the scene. Now whenever the view or the scene decides that something needs to be repainted then the paint method is being called. And it is the scene/view that initializes and passes the painter object to your code. No need to worry about that.

    Joh

  2. #2
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default Re: add points/path to QGraphicsView

    all right, thank you a lot, I'll come back soon with the errors...
    but honestly thanks, I think now I get it. off to try.


    Added after 6 minutes:


    Bearhug for JohannesMunk!

    For some similarly beginner's interest the whole code together:
    the derived class declaration:
    Qt Code:
    1. class myQGraphichsItem : public QGraphicsItem
    2. {
    3. private:
    4. QRect bRect;
    5. public:
    6. myQGraphichsItem();
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    8. QRectF boundingRect() const{ return bRect; }
    9. void setBoundingRect(int x1,int y1,int x2,int y2){bRect = QRect(x1,y1,x2,y2);}
    10. };
    To copy to clipboard, switch view to plain text mode 
    the paint method reimplemented:
    Qt Code:
    1. void myQGraphichsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->drawRect(boundingRect());
    4. }
    To copy to clipboard, switch view to plain text mode 
    in useage:
    Qt Code:
    1. myQGraphichsItem *mgi = new myQGraphichsItem;
    2. mgi->setBoundingRect(30,60,90,100);
    3. scene->addItem(mgi);
    4. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 
    Last edited by szisziszilvi; 13th January 2011 at 14:34.
    Szilvi

Similar Threads

  1. Replies: 1
    Last Post: 21st December 2010, 23:04
  2. Points selection
    By CCTeam in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2010, 15:22
  3. Replies: 8
    Last Post: 17th October 2009, 09:10
  4. drawing an Arc using points
    By chethana in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2009, 04:59
  5. Plotting points
    By afflictedd2 in forum Qt Programming
    Replies: 8
    Last Post: 26th February 2009, 09:20

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
  •  
Qt is a trademark of The Qt Company.