Results 1 to 7 of 7

Thread: add points/path to QGraphicsView

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

    Default add points/path to QGraphicsView

    Hello,

    I'm on to learn the usage of QGraphicsView, and the main goal would be to draw diagrams of some data, so the best would be if I could give points (or better dreams: paths) to the object.

    Now this works fine:

    Qt Code:
    1. scene->addLine(0,0,50,30,QPen(QColor(0,0,0)));
    2. scene->addEllipse(10,20,50,60);
    3. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 


    but I can't figure out how could I add simple points, or how to combine it with eg. QPainter. Or if I'm rihgt there would be a way through addItem, but I'm not very familiar with it's logic, and the examples in the Help are too complicated to avoid losting in the forest.

    Regards
    Szilvi

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

    Default Re: add points/path to QGraphicsView

    Hello Szilvi!

    Have you considered using qwt to draw diagrams?

    If you insist on doing it yourself in a graphicsview, create a class derived from QGraphicsItem and reimplement the paint function. The paint function recieves a pointer to a painter, which you can use to draw your diagram. If you have calculated the diagram-points you can draw them using drawPolyline or drawPoints.

    HIH

    Johannes

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

    Default Re: add points/path to QGraphicsView

    Yes, actually I tried to use this qwt, I downloaded, but somehow failed to install, I just couldn't reach it. But yes, you are far right that it could be better. Anyway it is never too bad for a beginner to learn the logic of the whole system.
    Do I understand your idea well? Like this:
    Qt Code:
    1. QGraphicsItemChild gic;
    2. QPainter *painter;
    3. painter.drawPoints(...); //(ect.)
    4. gic.paint(painter);
    5. scene->addItem(gic);
    To copy to clipboard, switch view to plain text mode 
    Well, something must be not clear for me. Cheking the Help I can't see how could I calculate the coordinates themselves outside the reimplementation. Inside the reimplementation they suggest:

    painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);

    But in this case the coordinates must be given inside.

    Another question: if I create a clas derived for QGraphicsItem, and try to use that in the addItem function, that will give an error, won't it?
    Szilvi

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

    Default Re: add points/path to QGraphicsView

    Hi Szilvi!

    No, sorry to disappoint, but you didn't get it right :-> Creating a subclass and reimplementing a function looks very different! I recommend reading a c++ book first!

    Qt Code:
    1. class Diagram : public QGraphicsItem
    2. {
    3. public:
    4. Diagram() {}
    5.  
    6. QRectF boundingRect() const{ return QRectF(0, 0, 50, 50); }
    7.  
    8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    9. {
    10. painter->drawRect(boundingRect());
    11. painter->drawPolyline(...)
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    Now you can create an instance of this diagram-class and add it to the scene.

    Qt Code:
    1. Diagram d = new Diagram();
    2. scene->addItem(d);
    To copy to clipboard, switch view to plain text mode 

    You might want to pass the data as argument to its constructor or some setup-function.

    This works as follows: The scene knows, that all its items are QGraphicItems. When it needs to redraw an item it simply calls its paint-method. And as we reimplemented that paint-method in our derived class, our code gets called.

    HIH

    Johannes

  5. The following user says thank you to JohannesMunk for this useful post:

    szisziszilvi (13th January 2011)

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

    Default Re: add points/path to QGraphicsView

    Yes, in my example I used a fake type: QGraphicsItemChild, where the reimplementation would be done.
    So from your suggestion it would be like this:
    there would be a member variable like
    QRect bRect;
    there would be a public member in Diagram:
    void setBoundingRect(int x1,int y1,int x2,int y2){bRect = QRect(x1,y1,x2,y2);}
    from yours the line: QRectF boundingRect() const{ return QRectF(0, 0, 50, 50); } turns to
    QRectF boundingRect() const{ return bRect; }

    so the bRect can be set from outside, and then at usage:

    Diagram d = new Diagram;
    d.setBoundingRect(1,2,3,4);
    scene->addItem(d);

    And here addItem calls the paint method by default? But for what QPainter object? Doesn't there should be a QPainter somewhere?
    Szilvi

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

    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

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

    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 13:34.
    Szilvi

Similar Threads

  1. Replies: 1
    Last Post: 21st December 2010, 22:04
  2. Points selection
    By CCTeam in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2010, 14:22
  3. Replies: 8
    Last Post: 17th October 2009, 08:10
  4. drawing an Arc using points
    By chethana in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2009, 03:59
  5. Plotting points
    By afflictedd2 in forum Qt Programming
    Replies: 8
    Last Post: 26th February 2009, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.