Results 1 to 4 of 4

Thread: Adding Custom Variable To QGraphicsItem, Possible?

  1. #1
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Adding Custom Variable To QGraphicsItem, Possible?

    Hello,

    I am looking for a way to create a new variable for a qgraphicsitem. I need to set it for save and load purposes but how would i go about doing this?

    The end result should be something along these lines:
    QGraphicsItem *temp;
    temp = scene->addPixmap([blah blah blah]);
    temp->customvariable = 1; //This is the variable i want to create (int) for each time i add the graphicsitem

    Thanks in advance and sorry if i did not explain this very well but it is late.
    Matt

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding Custom Variable To QGraphicsItem, Possible?

    Subclass QGraphicsItem, or one of its existing subclasses (QGraphicsPixmapItem in your example), and go to town. Standard C++ stuff.

  3. #3
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Adding Custom Variable To QGraphicsItem, Possible?

    Thanks for the help man. You have pointed me in the right direction which has helped alot.
    Qt Code:
    1. class TileItem : public QObject, public QGraphicsPixmapItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. int mynewvariable();
    7. void setmynewvariable(int a);
    8. };
    To copy to clipboard, switch view to plain text mode 

    Then for the setmynewvariable function:
    Qt Code:
    1. void TileItem::setmynewvariable(int a)
    2. {
    3. this->mynewvariable = a;
    4. }
    To copy to clipboard, switch view to plain text mode 

    But whenever i try to add this item to my graphicsscene:
    Qt Code:
    1. TileItem *pix;
    2. scene->addItem(pix);
    To copy to clipboard, switch view to plain text mode 

    It simply will not allow me. It crashed the whole program. Is there something i am overlooking?

    Thanks again for the help,
    Matt

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

    Default Re: Adding Custom Variable To QGraphicsItem, Possible?

    You created a pointer to a TileItem object but you did not initialize it with a real object. Still standard C++ stuff.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Custom QGraphicsItem - Adding a QListWidget
    By El Bazza in forum Qt Programming
    Replies: 8
    Last Post: 12th September 2012, 16:19
  2. Replies: 8
    Last Post: 29th July 2012, 01:28
  3. Replies: 2
    Last Post: 3rd April 2007, 19:47
  4. adding QGraphicsItem to multiple scenes
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2006, 14:25
  5. custom plugin designer property with out a variable?
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:11

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
  •  
Qt is a trademark of The Qt Company.