PDA

View Full Version : HowTo: Draw an object I made.



alitoh
17th March 2011, 19:18
Hello,

My problem is this:

I'm trying to make a cross-plot (reference (http://www.ikonscience.com/img/rokdoc-ppc/result_crossplot.jpg)) and I've come to the point where I want to draw my QList<Node> listOfNodes.

My node class is (something) like this



#ifndef CROSSPLOTNODE_H
#define CROSSPLOTNODE_H


#include <QtGui>
#include <QtGui/QColor>
#include <QtGui/QGraphicsItem>
#include "Labwell.h"
#include "AbstractNode.h"


class CrossPlotNode : public QGraphicsItem
{
public:
CrossPlotNode(LabWell *well, QString Cx, QString Cy, QGraphicsScene *parent);
~CrossPlotNode();

void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
void setPen(const QPen);
void setColor(const QColor);
void setStyle(Qt::PenStyle style);
void setPenWidth(qreal width);

private:
LabWell *_well;
QString _Cx;
QString _Cy;
QList<AbstractNode> nodeList;
QPen m_pen;
};
#endif
and, just in case, abstractNode is something like this:



#ifndef ABSTRACTNODE_H
#define ABSTRACTNODE_H

#include <QtGui>

class AbstractNode : public QGraphicsItem
{
public:
void setValues(QPointF points);

QPointF getValues();

QRectF boundingRect() const;

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverMoveEvent(QGraphicsSceneHoverEvent *event);

private:
qreal depth;
QPointF values;
};

#endif
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 (http://www.qtcentre.org/threads/620-Howto-draw-stuff) 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.

stampede
17th March 2011, 19:43
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:


QImage image(size,format);
QPainter painter(&image);
foreach( const CrossPlotNode& n, nodes ){
n.paint(&painter,NULL,NULL);
}

If you want to display your items in GUI, you need to have QGraphicsView with attached QGraphicsScene, where you'll add your items:


QGraphicsView * view = new QGraphicsView();
QGraphicsScene * scene = new QGraphicsScene();
view->setScene(scene);
for( int i=0 ; i<nodes.count() ; ++i ){
scene->addItem(&nodes[i]); //!< for this I prefer to create items on heap, but this should work
}
view->show();

You may want to refer to QGraphicsView and QGraphicsScene documentation for further info.