PDA

View Full Version : Questions about QGraphicsItems



NoRulez
22nd April 2008, 10:04
Hey @all,

i would like to draw a QPixmapItem and under the pixmap i would like to have a QGraphicsTextItem. I can move each item (QPixmapItem and QGraphicsTextItem) and each of them has a border when i select one of these.

But how can i do that, so when i move a QPixmapItem, that the QGraphicsItem moves also?
I 've tried to do so with a QGraphicsItemGroup, but here is the problem when i select an item, then all items (QPixmapItem and QGraphicsTextItem) have a selection border but i will have only one border.

Can anybody help please?

Kind Regards
NoRulez

aamer4yu
22nd April 2008, 10:11
What are you trying to achieve ? If u just need to display text, u can very well do it in QPIxmapItem too. ??

You can do one thing - when the pixmapitem moves, catch the move event of the pixmapitem, calculate the offset by which it has moved.
now from this moveEvent() function, send the owner of items to move the textitem by the offset calculated.

Is this u wanted to achieve ? or am i missing something ?

NoRulez
22nd April 2008, 10:56
The goal is to draw the following:


+-----------------+ +-----------------+
| | | |
| |[]------\ | |[] - - - -
| PIXMAP | \-[]| PIXMAP | OTHER OBJECTS
| |[] | |[] - - - -
| | | |
+-----------------+ +-----------------+
Object Nr.1 Object_2


Each Object has 0 - n connectors on each side, in the middle a QGraphicsPixmapItem and under the object in the middle the Object name.
Between the connectors i would like to add a line which moves also if one of the connected objects moves (like Visio)

Currently i have the QGraphicsPixmapItem implemented, but i don't know how to implement the QGraphicsTextItem and the lines between the connectors and the connectors self. :(

@aamer4yu: I tried to put the following in my class (inherited from QGraphicsPixmapItem) as follows:


DiagramImageItem::DiagramImageItem(DiagramType diagramType, QMenu *contextMenu,
QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsPixmapItem(parent, scene)
{

connect(scene, SIGNAL(retrieveItemData(const QString&, const QVariant&)),
this, SLOT(retrieveItemData(const QString&, const QVariant&)));
obj_id++;
myDiagramType = diagramType;
myContextMenu = contextMenu;

this->obj_name = object_name.toLower();
this->obj_type_str = object_type;
this->obj_type = myDiagramType;
setToolTip(this->obj_name);
setObjectName(object_name);

setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);

textItem = new DiagramTextItem();
textItem->setPlainText(this->obj_name);
textItem->setFont(QFont("courier"));//myFont);
//textItem->setTextInteractionFlags(Qt::TextEditorInteraction) ;
textItem->setZValue(this->zValue());
textItem->setDefaultTextColor(QColor(0, 0, 0));//myTextColor);
textItem->setPos(this->pos().x()+20, this->pos().y()+100);//+266);//mouseEvent->scenePos());
this->scene()->addItem(textItem);
// addItem(textItem);
}


Put i didn't see any text. As base of the code i used the diagram example from QT 4.3

Kind Regards
NoRulez

wysota
23rd April 2008, 00:19
Make one item child of the other. Then make the parent selectable and movable using proper item flags.

aamer4yu
23rd April 2008, 07:54
Have a look at the Diagrame Scene Example in QtDemo under GraphicsView section.

I guess thats something similar you are trying to achieve :)

NoRulez
8th May 2008, 09:42
OK,

if have it running :)

But the problem now is, when i have multiple objects with lines connected, my CPU is around 80% increasing.

The only way i can do in the moment is to close the apllication, so my CPU is ddecreasing to (0 - 10 % ==> normal CPU usage).

Whats going on there?
If also noticed that the paint event in the "arrow.cpp" is called to often.

Can anybody help me?

Regards
NoRulez

vycke
8th May 2008, 19:01
I was able to put a "label" on a QGraphicPixmapItem by doing the following:



class DeviceItem: public QGraphicsPixmapItem
{
public
DeviceItem(DevData* data, QMenu *contextMenu, QGraphicsItem *parent = 0);

private:
QGraphicsSimpleTextItem* m_pText;
};


and



DeviceItem::DeviceItem(DevData* data, QMenu* contextMenu, QGraphicsItem* parent) :
QGraphicsPixmapItem(QPixmap(data->getIcon()), parent), m_pText(NULL), m_Menu(contextMenu)
{
setShapeMode(QGraphicsPixmapItem::BoundingRectShap e);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);

m_pText = new QGraphicsSimpleTextItem(data->getName(), this);
QRect rect = this->pixmap().rect();
QRectF rectF = m_pText->boundingRect();
int x = (rect.width()/2) - (rectF.width()/2);
m_pText->setPos(x, rect.bottom());
}


This puts the "label" centered underneath the pixmap. (As for arrow, that's the code to keep the items connected by the lines)

Vycke