Results 1 to 2 of 2

Thread: HowTo: Draw an object I made.

  1. #1
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default HowTo: Draw an object I made.

    Hello,

    My problem is this:

    I'm trying to make a cross-plot (reference) and I've come to the point where I want to draw my QList<Node> listOfNodes.

    My node class is (something) like this

    Qt Code:
    1. #ifndef CROSSPLOTNODE_H
    2. #define CROSSPLOTNODE_H
    3.  
    4.  
    5. #include <QtGui>
    6. #include <QtGui/QColor>
    7. #include <QtGui/QGraphicsItem>
    8. #include "Labwell.h"
    9. #include "AbstractNode.h"
    10.  
    11.  
    12. class CrossPlotNode : public QGraphicsItem
    13. {
    14. public:
    15. CrossPlotNode(LabWell *well, QString Cx, QString Cy, QGraphicsScene *parent);
    16. ~CrossPlotNode();
    17.  
    18. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
    19. void setPen(const QPen);
    20. void setColor(const QColor);
    21. void setStyle(Qt::PenStyle style);
    22. void setPenWidth(qreal width);
    23.  
    24. private:
    25. LabWell *_well;
    26. QString _Cx;
    27. QString _Cy;
    28. QList<AbstractNode> nodeList;
    29. QPen m_pen;
    30. };
    31. #endif
    To copy to clipboard, switch view to plain text mode 
    and, just in case, abstractNode is something like this:

    Qt Code:
    1. #ifndef ABSTRACTNODE_H
    2. #define ABSTRACTNODE_H
    3.  
    4. #include <QtGui>
    5.  
    6. class AbstractNode : public QGraphicsItem
    7. {
    8. public:
    9. void setValues(QPointF points);
    10.  
    11. QPointF getValues();
    12.  
    13. QRectF boundingRect() const;
    14.  
    15. protected:
    16. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    17. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    18. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    19. void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
    20.  
    21. private:
    22. qreal depth;
    23. QPointF values;
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 
    As you can see, they are data storage and handling classes. Now I want to draw CrossPlotNode from a CrossPlotView class (because, I believe, that's how I should do it. QGraphicsItems are graphic objects containers and something like a view, maybe? -I normally call it canvas, "A place where stuff can be drawn", not really sure how it's called in Qt...- I think it's a QGraphicsView which is a graphics display?).

    That's pretty much how my classes, but I just don't understand how to draw something.

    As a simple reference, I'd like to grab a node's boundingRect and use it to draw a rectangle in my View with those very same values (posX, posY, h, w).

    If I'm not clear, please do let me know. I'm very new at QT (never really worked on GUI's) and it wouldn't surprise me if I wasn't clear enough.

    Thanks in advance,
    Alitoh

    EDIT
    I am aware that AbstractNode does NOT have a constructor. It's just that I haven't made up my mind about how it will work, to a certain degree because of this particular inquiry.
    Just wanted to clarify that.

    EDIT2
    I just read this and, albeit EXTREMELY helful (and might shed some light into future possible questions) does not answer my question. I'll add that I DO read the documentation but, sometimes, it's rather hard to follow and some things are not always quite clear.
    Last edited by alitoh; 17th March 2011 at 20:35.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: HowTo: Draw an object I made.

    but I just don't understand how to draw something.
    It depends on where you want to draw it. Basically, you can setup a QPainter on any paint device and paint your items by hand:
    Qt Code:
    1. QImage image(size,format);
    2. QPainter painter(&image);
    3. foreach( const CrossPlotNode& n, nodes ){
    4. n.paint(&painter,NULL,NULL);
    5. }
    To copy to clipboard, switch view to plain text mode 
    If you want to display your items in GUI, you need to have QGraphicsView with attached QGraphicsScene, where you'll add your items:
    Qt Code:
    1. view->setScene(scene);
    2. for( int i=0 ; i<nodes.count() ; ++i ){
    3. scene->addItem(&nodes[i]); //!< for this I prefer to create items on heap, but this should work
    4. }
    5. view->show();
    To copy to clipboard, switch view to plain text mode 
    You may want to refer to QGraphicsView and QGraphicsScene documentation for further info.

  3. The following user says thank you to stampede for this useful post:

    alitoh (17th March 2011)

Similar Threads

  1. Replies: 1
    Last Post: 5th October 2010, 18:08
  2. Replies: 2
    Last Post: 14th April 2008, 12:03
  3. Replies: 2
    Last Post: 18th March 2008, 12:47
  4. Replies: 5
    Last Post: 11th March 2008, 14:38
  5. Howto draw stuff
    By Morea in forum Newbie
    Replies: 16
    Last Post: 7th April 2006, 13:05

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.