Re: QSet and custom class
You made a list of Alarms but you tried to insert a pointer to an Alarm.
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?
Re: QSet and custom class
if (please note the big if here!) Alarm indeed is copy-constructible
Code:
QSet<Alarm> alarms; // don't call a set alarmList...
alarms.insert(Alarm());
maybe you want
Code:
QSet<Alarm*> alarms;
alarms.insert(new Alarm);
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.
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:
Code:
{
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...
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.
Code:
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).
Re: QSet and custom class
Quote:
Originally Posted by
been_1990
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.
Quote:
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.
Re: QSet and custom class
Quote:
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.
Re: QSet and custom class
Code:
QMap<QString, Alarm*> map;
if(map.contains("some name")){
qWarning("Alarm already exists");
} else {
map.insert(alarm->name(), alarm);
}
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.
Re: QSet and custom class
QSet is a QMap (or QHash but that's functionally irrelevant) without a value.
Re: QSet and custom class
It's working wonderfully now.