Results 1 to 3 of 3

Thread: How to make a QSet hold structs?

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default How to make a QSet hold structs?

    I'm a bit confused, because I thought QSet could hold structs, but I keep getting an error "c:\QtSDK\Desktop\Qt\4.7.4\mingw\include\QtCore\qh ash.h:880: error: no matching function for call to 'qHash(const NoteStruct&)'"

    Here's the struct:

    Qt Code:
    1. struct NoteStruct
    2. {
    3. float startPosition;
    4. float width;
    5. QGraphicsItem *noteLane;
    6. int velocity;
    7. };
    To copy to clipboard, switch view to plain text mode 

    Here's the offending method:

    Qt Code:
    1. void SequencerScene::copy()
    2. {
    3. currentlyCopiedNotes.clear();
    4.  
    5. foreach (QGraphicsItem *note, selectedItems())
    6. {
    7. NoteStruct newNote;
    8. newNote.startPosition = note->transform().dx();
    9. newNote.width = note->boundingRect().width();
    10. newNote.noteLane = note->parentItem();
    11. newNote.velocity = static_cast<Note*>(note)->getVelocity();
    12.  
    13. currentlyCopiedNotes.insert(newNote); // THIS IS THE OFFENDING CODE
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    And here's the declaration of currentlyCopiedNotes (in the header file):

    Qt Code:
    1. private:
    2. QSet<NoteStruct> currentlyCopiedNotes;
    To copy to clipboard, switch view to plain text mode 

    The reason I'm trying to do this is that I am creating a way to copy notes (QGraphicsItems) in a QGraphicsScene, so that they can be pasted afterwards. The NoteStruct is a struct containing the attributes of the notes.

    I'd appreciate some advice... thanks!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to make a QSet hold structs?

    Your type needs to have a operator==() and also a global qHash(const NoteStruct&)
    You can see the reference page here

  3. #3
    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: How to make a QSet hold structs?

    ...and be wary of the floats if they are part of your equality test.

Similar Threads

  1. QSet of QDateTime
    By Cucumber in forum Newbie
    Replies: 2
    Last Post: 21st January 2012, 02:17
  2. Structs for C++ and Qt
    By therealjag in forum General Programming
    Replies: 8
    Last Post: 6th June 2011, 11:56
  3. Can QVector hold objects?
    By N3wb in forum Newbie
    Replies: 6
    Last Post: 14th April 2010, 20:29
  4. checkable QAction shortcut hold
    By winder in forum Qt Programming
    Replies: 7
    Last Post: 18th February 2010, 15:48
  5. QTextEdit does not hold Object Id behind
    By validator in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2008, 00:25

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