PDA

View Full Version : Custom QGraphicsItems



ReasyEasyPeasy
8th August 2015, 23:27
Hey guys,
first of all english isn't my first language.
I got some problems to creat my own QGraphicsItem. First of all i will show u what i got already:

postit.h

#ifndef POSTIT_H
#define POSTIT_H

#include <QGraphicsItem>
#include <QPainter>
#include <QRectF>
#include <QTextItem>
#include <QGraphicsTextItem>


class PostIt : public QGraphicsItem
{
public:
PostIt(QGraphicsItem* parent = NULL);

protected:
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget);
QRectF boundingRect() const;
};

#endif // POSTIT_H
postit.cpp:

#include "../header/postit.h"

PostIt::PostIt(QGraphicsItem* parent) : QGraphicsItem(parent){


setFlag(QGraphicsItem::ItemIsMovable);


}

QRectF PostIt::boundingRect() const{
return QRectF(0,0,400,400);
}
void PostIt::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setPen(Qt::black);
painter->setBrush(Qt::yellow);
painter->drawRoundedRect(0,0,400,400,5,5);
painter->drawRoundedRect(0,0,200,100,5,2);

}

}
This one looks exactly like i want. But i need to draw text, which is editable. I tried so much with QGraphicsTextItems and searched in the internet. I found some things, but i dont understand how i can draw this GraphicstextItem in my rect.
I addition, i want to display a picture in the rect. It would be perfect if the user can just drag and drop the picture in my rect. Maybe u can give me some tipps for this problem too.
thanks for reading! :)

anda_skoa
9th August 2015, 10:26
You can put any item "on top" of your item by using your item as the parent item.

Cheers,
_

ReasyEasyPeasy
10th August 2015, 21:11
Hey,
i tried:


QRectF tesst(0,0,40,40);
painter->drawRect(tesst);
QGraphicsTextItem *test = new QGraphicsTextItem(tesst);
test->setPlainText("bla");
I created a rect and made this rect to the parent of my GraphicsTextItem. It dont work.
It would be nice if you could give me a small code example how u would do this. :)
kind regards

anda_skoa
10th August 2015, 22:27
That doesn't even compile.

I suggest to have a look at the documentation of QGraphicsTextItem and then think about what I could have possibly referred to when writing "using your item as the parent item"

Cheers,
_

ReasyEasyPeasy
11th August 2015, 15:50
Hey,
i really really don't understand it. I always look into the documentation before i ask "stupid" questions. But im trying since 4 days only to draw editable text in a box. I can draw text with "painter->drawText(0,0,"Test");". I dont understand why i cant draw a QGraphicsTextItem. I also tried to creat an other QGraphicsItem and draw it in my PostIt QGraphicsItem. It dont work how i try it. I watched some tutorials in the internet, but they are just drawing some rects and triangles... . Im not an informatic student. Im doing this for fun and sometimes i just need more help than other people.
I dont want a comeplete code. I want to write it by my own because i like doing it, but at this point i can't do it without more help.


Please help me a bit more:).

kind regards

anda_skoa
11th August 2015, 17:49
Basically the items in a scene form a tree.

This tree is constructed by passing the pointer of one item as the "parent" of any number of other items.
These items become the "parent" item's "children".

Children are positioned relative to their parent, if you move such a parent item, the children move along with it.

After an items is drawn and its children are then drawn "on top" of it.

So you don't draw a text item on your item, you simply create a text item as a child of your item.
By passing your item as the parent to the text item's constructor, as I wrote in comment #2.



PostIt::PostIt(QGraphicsItem* parent) : QGraphicsItem(parent){
setFlag(QGraphicsItem::ItemIsMovable);

QGraphicsTextItem *text = new QGraphicsTextItem("Hello World", this);
}


Cheers,
_

ReasyEasyPeasy
12th August 2015, 10:17
Hey,
thank you so much thats exactly what i needed. I got one more question.
I created a second QGraphicsItem:

#ifndef DRAGDROPRECT_H
#define DRAGDROPRECT_H
#include <QGraphicsItem>
#include <QPainter>
#include <QRectF>
#include <QTextItem>
#include <QGraphicsTextItem>

class DragDropRect : public QGraphicsItem
{
public:
DragDropRect(QGraphicsItem* parent = NULL);



protected:
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget);
QRectF boundingRect() const;
void dropEvent(QGraphicsSceneDragDropEvent *event);

};

#endif // DRAGDROPRECT

#include "../header/dragdroprect.h"

DragDropRect::DragDropRect()
{
setAcceptDrops(true);
}



QRectF DragDropRect::boundingRect() const{
return QRectF(0,0,200,200);
}

void DragDropRect::dropEvent(QGraphicsSceneDragDropEven t *event)
{

}
void DragDropRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

painter->setBrush(Qt::gray);
painter->drawRoundedRect(0,0,200,100,5,2);
}

and i tryed to get this into my PostIt item. So as i learned i took this dragdropRect and made PostIt to his parent in the PostIt constructor.

DragDropRect *rect = new DragDropRect(this);
Well it doesnt even compile.

I want to say thank you i really appreciate it that you spend your time to help me! :)

anda_skoa
12th August 2015, 10:32
Well it doesnt even compile.


If you compare the signature of the constructor between the header and the source file, you will see they don't match (different number of arguments)
Looks at how you've done it in postit.cpp

Cheers,
_