PDA

View Full Version : QSet and custom class



been_1990
22nd January 2010, 01:13
When I try to use QSet and a custom class :


QSet<Alarm> alarmList;
Alarm *a = new Alarm();
alarmList.insert(a);

I get the following error:


error: no matching function for call to 'QSet<Alarm>::insert(Alarm*&)'
note: candidates are: QSet<T>::const_iterator QSet<T>::insert(const T&) [with T = Alarm]

Coises
22nd January 2010, 02:50
You made a list of Alarms but you tried to insert a pointer to an Alarm.

been_1990
22nd January 2010, 11:35
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?

caduel
22nd January 2010, 13:15
if (please note the big if here!) Alarm indeed is copy-constructible

QSet<Alarm> alarms; // don't call a set alarmList...
alarms.insert(Alarm());

maybe you want

QSet<Alarm*> alarms;
alarms.insert(new Alarm);

wysota
22nd January 2010, 13:33
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.

been_1990
22nd January 2010, 13:36
So.. the * after Alarm makes QSet expect the address of a Alarm...(here (http://www.boost.org/doc/libs/1_41_0/libs/utility/CopyConstructible.html)) I had no idea. What's the difference between *Alarm and Alarm*? (off topic, I know...)
This is how I'm using it:


addActiveAlarm(QString file)
{
Alarm *a = new Alarm(file);
alarmList.insert(a);
connect(a,SIGNAL(soundAlarm()),display,SLOT(show() ));
}

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...

caduel
22nd January 2010, 15:28
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.


Alarm a; // an (default-constructed) object of type Alarm
Alarm *pa; // an uninitialized pointer to an object of type Alarm
pa = &a; // assigning the address of a to pa
if (pa==NULL) // testing the pointer
(*pa). .... // accessing the object pointed to by pa
pa-> // same as (*pa).

wysota
22nd January 2010, 15:38
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.

been_1990
22nd January 2010, 15:51
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.

wysota
22nd January 2010, 16:02
QMap<QString, Alarm*> map;
if(map.contains("some name")){
qWarning("Alarm already exists");
} else {
map.insert(alarm->name(), alarm);
}

been_1990
22nd January 2010, 16:16
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.

wysota
22nd January 2010, 16:26
QSet is a QMap (or QHash but that's functionally irrelevant) without a value.

been_1990
22nd January 2010, 16:36
It's working wonderfully now.