Results 1 to 7 of 7

Thread: QPair<QDate, int>

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QPair<QDate, int>

    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.

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPair<QDate, int>

    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?

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPair<QDate, int>

    Thing is - it's not vorking, but probably because I'm not using QPair correctly, here is a bit of code:
    datamanager.cpp
    Qt Code:
    1. void DataManager::addTaskItem(QDate *date, TaskItem *taskItem)
    2. {
    3. //taskItemList.append(taskItem);
    4. list->append(qMakePair(date, taskItem));
    5. bool complete = false;
    6. for(int i=0; i<hoursList->size(); i++)
    7. {
    8. if (hoursList->value(i).first==date)
    9. {
    10. hours = hoursList->value(i).second;
    11. hours = hours + taskItem->getHourStr().toInt();
    12. hoursList->replace(i, qMakePair(date, hours));
    13. complete = true;
    14. }
    15. if (complete == true)
    16. break;
    17. }
    18.  
    19. if (complete == false)
    20. {
    21. hours = taskItem->getHourStr().toInt();
    22. hoursList->append(qMakePair(date, hours));
    23. }
    24. }
    25.  
    26. void DataManager::clearTaskItem(QDate *date)
    27. {
    28. for(int i=0; i<list->size(); i++)
    29. if (list->value(i).first==date)
    30. list->removeAt(i);
    31. for(int i=0; i<hoursList->size(); i++)
    32. if (hoursList->value(i).first==date)
    33. hoursList->removeAt(i);
    34. }
    To copy to clipboard, switch view to plain text mode 

    customCalendarWidget.cpp
    Qt Code:
    1. QString CustomCalendarWidget::workHours(QDate date) const
    2. {
    3. QDate *dateForCalendar = new QDate(date);
    4. QString string("0");
    5. for(int i=0; i<hoursList->size(); i++)
    6. {
    7. if (hoursList->value(i).first==dateForCalendar)
    8. string.setNum(hoursList->value(i).second);
    9. }
    10. return string;
    11. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPair<QDate, int>

    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?

  5. #5
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPair<QDate, int>

    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*???

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPair<QDate, int>

    You are comparing the value of the pointer not the value of the date object that it points to:
    Qt Code:
    1. QDate *a = new QDate(2011, 1, 1);
    2. QDate *b = new QDate(2011, 1, 1);
    3. qDebug() << "Compare the pointers" << (a == b); // false, the two pointers are different i.e. they do not point at the same object
    4. qDebug() << "Compare the objects" << (*a == *b); // true, the objects pointed at have the same value
    To copy to clipboard, switch view to plain text mode 

    Why use heap allocated QDates in the first place? You can quite happily store QDate in a QPair:
    Qt Code:
    1. QList<QPair<QDate, int> > pairs;
    2. pairs << qMakePair(QDate::currentDate(), 1);
    3. pairs << qMakePair(QDate::currentDate(), 1);
    4. qDebug() << pairs.count() << pairs.at(0).first << pairs.at(1).first
    5. << (pairs.at(0).first == pairs.at(1).first );
    6. // outputs:
    7. // 2 QDate("Thu May 19 2011") QDate("Thu May 19 2011") true
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    Archa4 (19th May 2011)

  8. #7
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPair<QDate, int>

    Thanks! I threw out
    Qt Code:
    1. QDate *dateForCalendar = new QDate(date);
    To copy to clipboard, switch view to plain text mode 
    And in the if statement I just did
    Qt Code:
    1. if (*(hoursList->at(i).first)==date)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 22nd December 2010, 10:37
  2. Using QPair in QTest
    By hackerNovitiate in forum Newbie
    Replies: 4
    Last Post: 16th November 2010, 05:42
  3. How to qSort a Qlist of QPair?
    By Trader in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2010, 19:04
  4. Replies: 4
    Last Post: 12th October 2009, 19:36
  5. using QPair in QVector
    By damonlin in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2009, 09:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.