PDA

View Full Version : QPair<QDate, int>



Archa4
18th May 2011, 16:11
I know that I cannot Use this, so I found out I have to use either pointers, or Const.
I think My pointers are not working, so I want to use const, but I have no idea how. I know this has little to do with Qt, but I really have no time, so, please, if anyne can help, could U tell me, how to and when to initialize those const QDate &date, and how to use the properly.

FelixB
18th May 2011, 16:22
please provide some code... I don'tt like talking about specific problems in a very theoretical way.

whats wrong with your pointers? why shouldn't they be not working?

Archa4
19th May 2011, 08:19
Thing is - it's not vorking, but probably because I'm not using QPair correctly, here is a bit of code:
datamanager.cpp

void DataManager::addTaskItem(QDate *date, TaskItem *taskItem)
{
//taskItemList.append(taskItem);
list->append(qMakePair(date, taskItem));
bool complete = false;
for(int i=0; i<hoursList->size(); i++)
{
if (hoursList->value(i).first==date)
{
hours = hoursList->value(i).second;
hours = hours + taskItem->getHourStr().toInt();
hoursList->replace(i, qMakePair(date, hours));
complete = true;
}
if (complete == true)
break;
}

if (complete == false)
{
hours = taskItem->getHourStr().toInt();
hoursList->append(qMakePair(date, hours));
}
}

void DataManager::clearTaskItem(QDate *date)
{
for(int i=0; i<list->size(); i++)
if (list->value(i).first==date)
list->removeAt(i);
for(int i=0; i<hoursList->size(); i++)
if (hoursList->value(i).first==date)
hoursList->removeAt(i);
}


customCalendarWidget.cpp

QString CustomCalendarWidget::workHours(QDate date) const
{
QDate *dateForCalendar = new QDate(date);
QString string("0");
for(int i=0; i<hoursList->size(); i++)
{
if (hoursList->value(i).first==dateForCalendar)
string.setNum(hoursList->value(i).second);
}
return string;
}

thing is - i can't get the
hoursList->value(i).first==dateForCalendar
to work.
I looked through the debugger, and when dateForCalendar has the same value as QPair->first value, I still don't go through that first 'if' in the customCalendarWidget.cpp. I think there is something wrong with that 'if statement...

Zlatomir
19th May 2011, 08:31
That first is a QDate* (if i assume correctly - from the first post) so then you compare QDate* with a QDate variable date (the workHours(QDate date) takes a QDate value not a pointer)

LE: ignore the above comment

Are you sure that you need the pointers to be equal? Shouldn't you compare the dereferenced pointers?

Archa4
19th May 2011, 08:33
Something has changed...
Now I don't get the match because when i look through the debugger the dataForCalendar is always 5. I have no idea why (the QDate date parameter in the customCalendarWidget is worth from 2455677 and up..). What am I doing wrong???

Ok, so now the question is:
how can I take the parameter that is simple QDate and compare it with QDate*???

ChrisW67
19th May 2011, 08:42
You are comparing the value of the pointer not the value of the date object that it points to:


QDate *a = new QDate(2011, 1, 1);
QDate *b = new QDate(2011, 1, 1);
qDebug() << "Compare the pointers" << (a == b); // false, the two pointers are different i.e. they do not point at the same object
qDebug() << "Compare the objects" << (*a == *b); // true, the objects pointed at have the same value


Why use heap allocated QDates in the first place? You can quite happily store QDate in a QPair:


QList<QPair<QDate, int> > pairs;
pairs << qMakePair(QDate::currentDate(), 1);
pairs << qMakePair(QDate::currentDate(), 1);
qDebug() << pairs.count() << pairs.at(0).first << pairs.at(1).first
<< (pairs.at(0).first == pairs.at(1).first );
// outputs:
// 2 QDate("Thu May 19 2011") QDate("Thu May 19 2011") true

Archa4
19th May 2011, 09:20
Thanks! I threw out

QDate *dateForCalendar = new QDate(date);
And in the if statement I just did

if (*(hoursList->at(i).first)==date)