Results 1 to 7 of 7

Thread: Questions about QGraphicsItems

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Questions about QGraphicsItems

    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
    Last edited by NoRulez; 22nd April 2008 at 10:05. Reason: Better title

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about QGraphicsItems

    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 ?

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Question Re: Questions about QGraphicsItems

    The goal is to draw the following:
    Qt Code:
    1. +-----------------+ +-----------------+
    2. | | | |
    3. | |[]------\ | |[] - - - -
    4. | PIXMAP | \-[]| PIXMAP | OTHER OBJECTS
    5. | |[] | |[] - - - -
    6. | | | |
    7. +-----------------+ +-----------------+
    8. Object Nr.1 Object_2
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. DiagramImageItem::DiagramImageItem(DiagramType diagramType, QMenu *contextMenu,
    2. QGraphicsItem *parent, QGraphicsScene *scene)
    3. : QGraphicsPixmapItem(parent, scene)
    4. {
    5.  
    6. connect(scene, SIGNAL(retrieveItemData(const QString&, const QVariant&)),
    7. this, SLOT(retrieveItemData(const QString&, const QVariant&)));
    8. obj_id++;
    9. myDiagramType = diagramType;
    10. myContextMenu = contextMenu;
    11.  
    12. this->obj_name = object_name.toLower();
    13. this->obj_type_str = object_type;
    14. this->obj_type = myDiagramType;
    15. setToolTip(this->obj_name);
    16. setObjectName(object_name);
    17.  
    18. setFlag(QGraphicsItem::ItemIsMovable, true);
    19. setFlag(QGraphicsItem::ItemIsSelectable, true);
    20.  
    21. textItem = new DiagramTextItem();
    22. textItem->setPlainText(this->obj_name);
    23. textItem->setFont(QFont("courier"));//myFont);
    24. //textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
    25. textItem->setZValue(this->zValue());
    26. textItem->setDefaultTextColor(QColor(0, 0, 0));//myTextColor);
    27. textItem->setPos(this->pos().x()+20, this->pos().y()+100);//+266);//mouseEvent->scenePos());
    28. this->scene()->addItem(textItem);
    29. // addItem(textItem);
    30. }
    To copy to clipboard, switch view to plain text mode 

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

    Kind Regards
    NoRulez

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Questions about QGraphicsItems

    Make one item child of the other. Then make the parent selectable and movable using proper item flags.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about QGraphicsItems

    Have a look at the Diagrame Scene Example in QtDemo under GraphicsView section.

    I guess thats something similar you are trying to achieve

  6. #6
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Questions about QGraphicsItems

    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

  7. #7
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about QGraphicsItems

    I was able to put a "label" on a QGraphicPixmapItem by doing the following:

    Qt Code:
    1. class DeviceItem: public QGraphicsPixmapItem
    2. {
    3. public
    4. DeviceItem(DevData* data, QMenu *contextMenu, QGraphicsItem *parent = 0);
    5.  
    6. private:
    7. };
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. DeviceItem::DeviceItem(DevData* data, QMenu* contextMenu, QGraphicsItem* parent) :
    2. QGraphicsPixmapItem(QPixmap(data->getIcon()), parent), m_pText(NULL), m_Menu(contextMenu)
    3. {
    4. setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
    5. setFlag(QGraphicsItem::ItemIsMovable, true);
    6. setFlag(QGraphicsItem::ItemIsSelectable, true);
    7.  
    8. m_pText = new QGraphicsSimpleTextItem(data->getName(), this);
    9. QRect rect = this->pixmap().rect();
    10. QRectF rectF = m_pText->boundingRect();
    11. int x = (rect.width()/2) - (rectF.width()/2);
    12. m_pText->setPos(x, rect.bottom());
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    Vycke

Similar Threads

  1. Disable default tab behaviour for a QGraphicsItem
    By nmather in forum Qt Programming
    Replies: 3
    Last Post: 13th December 2017, 10:30
  2. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  3. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  4. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  5. QGraphicsItem with QSizeGrip
    By elsheikhmh in forum Newbie
    Replies: 7
    Last Post: 19th November 2009, 18:53

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.