Results 1 to 3 of 3

Thread: QMap with pointer to custom type

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default QMap with pointer to custom type

    I have a QMap of the form

    Qt Code:
    1. QMap< myCustomType1, myCustomType2* > myMap;
    To copy to clipboard, switch view to plain text mode 

    where myCustomType1 and myCustomType2 are both well-tested classes with all their constructors, assignment operators and destructors defined. I want to store pointers to objects of myCustomType2, rather than the objects themselves, since the objects actually reside in an array - I want to have the choice of searching for an object either by its array index, or by its associated key, without taking separate copies of the objects.

    However, when I try to populate the map:

    Qt Code:
    1. myMap.insert(myCustomType1Object, &myCustomType2Array[i]);
    To copy to clipboard, switch view to plain text mode 

    ...I always get an "Access violation reading location 0x00000000" thrown out form the QMap's 'detach' method (which is the first command in the source code for its 'insert' method).

    I've gone through the Qt documentation carefully and I can't see any restriction on the object types that can be put into a QMap (save that the key type must define operator<(), which mine does), so why doesn't this work?

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

    Default Re: QMap with pointer to custom type

    Is "this", the object containing the map, a valid instance?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMap with pointer to custom type

    Unbelievable! The solution really was that simple. I'd created my QMap as a pointer for no other reason than to avoid having to expose "#include <QMap>" in my header file, but then forgotten to instantiate it with 'new'.

    That fixed the problem. Thank you very much.

Similar Threads

  1. pointer to nested QMap and QList
    By franki in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2013, 11:44
  2. QMap only works when declared as a pointer
    By TheGrudge in forum Qt Programming
    Replies: 3
    Last Post: 10th February 2013, 10:12
  3. Q_PROPERTY of type QMap
    By eclarkso in forum Qt Programming
    Replies: 11
    Last Post: 30th October 2009, 18:43
  4. [solved] Qmap and pointer issue
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2007, 14:34
  5. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34

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.