Results 1 to 13 of 13

Thread: QSet and custom class

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QSet and custom class

    When I try to use QSet and a custom class :

    Qt Code:
    1. QSet<Alarm> alarmList;
    2. Alarm *a = new Alarm();
    3. alarmList.insert(a);
    To copy to clipboard, switch view to plain text mode 

    I get the following error:
    Qt Code:
    1. error: no matching function for call to 'QSet<Alarm>::insert(Alarm*&)'
    2. note: candidates are: QSet<T>::const_iterator QSet<T>::insert(const T&) [with T = Alarm]
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSet and custom class

    You made a list of Alarms but you tried to insert a pointer to an Alarm.

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSet and custom class

    SO how do I set it up? Since I use a function to create this "Alarm *a = new Alarm()", it has to be a pointer or else it gets lost after the function exits. Can I make a list of pointers to the Alarm's I create?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSet and custom class

    if (please note the big if here!) Alarm indeed is copy-constructible
    Qt Code:
    1. QSet<Alarm> alarms; // don't call a set alarmList...
    2. alarms.insert(Alarm());
    To copy to clipboard, switch view to plain text mode 

    maybe you want
    Qt Code:
    1. QSet<Alarm*> alarms;
    2. alarms.insert(new Alarm);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QSet and custom class

    The latter wouldn't make much sense, you wouldn't be able to compare one alarm to another object containing the same data because pointers would differ. Using objects is much better here.
    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.


  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSet and custom class

    So.. the * after Alarm makes QSet expect the address of a Alarm...(here) I had no idea. What's the difference between *Alarm and Alarm*? (off topic, I know...)
    This is how I'm using it:
    Qt Code:
    1. addActiveAlarm(QString file)
    2. {
    3. Alarm *a = new Alarm(file);
    4. alarmList.insert(a);
    5. connect(a,SIGNAL(soundAlarm()),display,SLOT(show()));
    6. }
    To copy to clipboard, switch view to plain text mode 

    I think that I need to create a "Alarm *a = new Alarm(file)", because I don't know if it's a good idea to connect a Alarm that's in QSet, or maybe it is...

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSet and custom class

    As Alarm is a QObject based class, you need to create it with new, because QObjects can not be copied.

    Wysota's point was: putting pointers inside a QSet does not (usually) make much sense: the set compares the pointers (unless you specify a special comparison function). So why not just use a QList<Alarm*>.

    The '*' means (depending on context): pointer, or dereference.

    Qt Code:
    1. Alarm a; // an (default-constructed) object of type Alarm
    2. Alarm *pa; // an uninitialized pointer to an object of type Alarm
    3. pa = &a; // assigning the address of a to pa
    4. if (pa==NULL) // testing the pointer
    5. (*pa). .... // accessing the object pointed to by pa
    6. pa-> // same as (*pa).
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: QSet and custom class

    Quote Originally Posted by been_1990 View Post
    What's the difference between *Alarm and Alarm*? (off topic, I know...)
    Basically none, they are equivalent. But it depends on the context really because the * operator has two meanings.

    I think that I need to create a "Alarm *a = new Alarm(file)", because I don't know if it's a good idea to connect a Alarm that's in QSet, or maybe it is...
    If your Alarm is a QObejct derived class, you can't store objects of this type in QSet, you can only store pointers. But then there is a question why do it in the first place.
    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.


  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSet and custom class

    If your Alarm is a QObejct derived class, you can't store objects of this type in QSet, you can only store pointers. But then there is a question why do it in the first place.
    Ok, I need a way to keep track of all alarms created so I wont create 15 alarms with the same name. So when I edit my alarm the same function that creates the new Alarm(), will first check to see if that alarm is already running, if it is it will just update the Alarm() info with the new values.
    Last edited by been_1990; 22nd January 2010 at 15:59.

  10. #10
    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: QSet and custom class

    Qt Code:
    1. QMap<QString, Alarm*> map;
    2. if(map.contains("some name")){
    3. qWarning("Alarm already exists");
    4. } else {
    5. map.insert(alarm->name(), alarm);
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.


  11. #11
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSet and custom class

    So the difference between QSet and QMap is: QSet stores just 1 value and unordered, while QMap "It stores (key, value) pairs and provides fast lookup of the value associated with a key." being ordered.

  12. #12
    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: QSet and custom class

    QSet is a QMap (or QHash but that's functionally irrelevant) without a value.
    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.


  13. #13
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSet and custom class

    It's working wonderfully now.

Similar Threads

  1. Replies: 11
    Last Post: 24th November 2009, 00:30
  2. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 06:30
  3. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 13:15
  4. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30

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.