Results 1 to 8 of 8

Thread: Custom QGraphicsItems

  1. #1
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Custom QGraphicsItems

    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
    Qt Code:
    1. #ifndef POSTIT_H
    2. #define POSTIT_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QPainter>
    6. #include <QRectF>
    7. #include <QTextItem>
    8. #include <QGraphicsTextItem>
    9.  
    10.  
    11. class PostIt : public QGraphicsItem
    12. {
    13. public:
    14. PostIt(QGraphicsItem* parent = NULL);
    15.  
    16. protected:
    17. void paint(QPainter *painter,
    18. const QStyleOptionGraphicsItem *option,
    19. QWidget *widget);
    20. QRectF boundingRect() const;
    21. };
    22.  
    23. #endif // POSTIT_H
    To copy to clipboard, switch view to plain text mode 
    postit.cpp:
    Qt Code:
    1. #include "../header/postit.h"
    2.  
    3. PostIt::PostIt(QGraphicsItem* parent) : QGraphicsItem(parent){
    4.  
    5.  
    6. setFlag(QGraphicsItem::ItemIsMovable);
    7.  
    8.  
    9. }
    10.  
    11. QRectF PostIt::boundingRect() const{
    12. return QRectF(0,0,400,400);
    13. }
    14. void PostIt::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    15. painter->setPen(Qt::black);
    16. painter->setBrush(Qt::yellow);
    17. painter->drawRoundedRect(0,0,400,400,5,5);
    18. painter->drawRoundedRect(0,0,200,100,5,2);
    19.  
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QGraphicsItems

    You can put any item "on top" of your item by using your item as the parent item.

    Cheers,
    _

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

    ReasyEasyPeasy (11th August 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom QGraphicsItems

    Hey,
    i tried:
    Qt Code:
    1. QRectF tesst(0,0,40,40);
    2. painter->drawRect(tesst);
    3. test->setPlainText("bla");
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by ReasyEasyPeasy; 10th August 2015 at 21:19.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QGraphicsItems

    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,
    _

  6. #5
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom QGraphicsItems

    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

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QGraphicsItems

    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.

    Qt Code:
    1. PostIt::PostIt(QGraphicsItem* parent) : QGraphicsItem(parent){
    2. setFlag(QGraphicsItem::ItemIsMovable);
    3.  
    4. QGraphicsTextItem *text = new QGraphicsTextItem("Hello World", this);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (12th August 2015)

  9. #7
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom QGraphicsItems

    Hey,
    thank you so much thats exactly what i needed. I got one more question.
    I created a second QGraphicsItem:
    Qt Code:
    1. #ifndef DRAGDROPRECT_H
    2. #define DRAGDROPRECT_H
    3. #include <QGraphicsItem>
    4. #include <QPainter>
    5. #include <QRectF>
    6. #include <QTextItem>
    7. #include <QGraphicsTextItem>
    8.  
    9. class DragDropRect : public QGraphicsItem
    10. {
    11. public:
    12. DragDropRect(QGraphicsItem* parent = NULL);
    13.  
    14.  
    15.  
    16. protected:
    17. void paint(QPainter *painter,
    18. const QStyleOptionGraphicsItem *option,
    19. QWidget *widget);
    20. QRectF boundingRect() const;
    21. void dropEvent(QGraphicsSceneDragDropEvent *event);
    22.  
    23. };
    24.  
    25. #endif // DRAGDROPRECT
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "../header/dragdroprect.h"
    2.  
    3. DragDropRect::DragDropRect()
    4. {
    5. setAcceptDrops(true);
    6. }
    7.  
    8.  
    9.  
    10. QRectF DragDropRect::boundingRect() const{
    11. return QRectF(0,0,200,200);
    12. }
    13.  
    14. void DragDropRect::dropEvent(QGraphicsSceneDragDropEvent *event)
    15. {
    16.  
    17. }
    18. void DragDropRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    19.  
    20. painter->setBrush(Qt::gray);
    21. painter->drawRoundedRect(0,0,200,100,5,2);
    22. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. DragDropRect *rect = new DragDropRect(this);
    To copy to clipboard, switch view to plain text mode 
    Well it doesnt even compile.

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

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QGraphicsItems

    Quote Originally Posted by ReasyEasyPeasy View Post
    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,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (12th August 2015)

Similar Threads

  1. Replies: 2
    Last Post: 8th December 2010, 10:51
  2. PopupMenu for several QGraphicsItems
    By jobrandt in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2009, 15:00
  3. QGraphicsItems Layers
    By qtuser20 in forum Qt Programming
    Replies: 7
    Last Post: 24th July 2009, 00:21
  4. Replies: 1
    Last Post: 10th September 2008, 16:49
  5. QGraphicsItems on top of each other?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2006, 20:23

Tags for this Thread

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.