Mosi
Mosi

Reputation: 1258

Issue with QMap and QDateTime as key

I'm using Qt 4.8.6 msvc 2010. I'm working on a software (this is done before Qt5, I'm supporting now). I have used a map:

QMap<QDateTime, quint32> dateTime

After I insert some data to this map (for example with 5000 of data) and I want to get a value of a specified key, then the returned value is not correct value. To make this clearer, suppose there are following top 20 items of the map:

            [0] Sun Dec 2 23:00:00 2018 0   
                key Sun Dec 2 23:00:00 2018 QDateTime
                value   0   unsigned int
            [1] Sun Dec 2 23:15:00 2018 1   
                key Sun Dec 2 23:15:00 2018 QDateTime
                value   1   unsigned int
            [2] Sun Dec 2 23:30:00 2018 2   
                key Sun Dec 2 23:30:00 2018 QDateTime
                value   2   unsigned int
            [3] Sun Dec 2 23:45:00 2018 3   
                key Sun Dec 2 23:45:00 2018 QDateTime
                value   3   unsigned int
            [4] (invalid)   4   
                key (invalid)   QDateTime
                value   4   unsigned int
            [5] Mon Dec 3 00:15:00 2018 5   
                key Mon Dec 3 00:15:00 2018 QDateTime
                value   5   unsigned int
            [6] Mon Dec 3 00:30:00 2018 6   
                key Mon Dec 3 00:30:00 2018 QDateTime
                value   6   unsigned int
            [7] Mon Dec 3 00:45:00 2018 7   
                key Mon Dec 3 00:45:00 2018 QDateTime
                value   7   unsigned int
            [8] Mon Dec 3 01:00:00 2018 8   
                key Mon Dec 3 01:00:00 2018 QDateTime
                value   8   unsigned int
            [9] Mon Dec 3 01:15:00 2018 9   
                key Mon Dec 3 01:15:00 2018 QDateTime
                value   9   unsigned int
            [10] Mon Dec 3 01:30:00 2018    10  
                key Mon Dec 3 01:30:00 2018 QDateTime
                value   10  unsigned int
            [11] Mon Dec 3 01:45:00 2018    11  
                key Mon Dec 3 01:45:00 2018 QDateTime
                value   11  unsigned int
            [12] Mon Dec 3 02:00:00 2018    12  
                key Mon Dec 3 02:00:00 2018 QDateTime
                value   12  unsigned int
            [13] Mon Dec 3 02:15:00 2018    13  
                key Mon Dec 3 02:15:00 2018 QDateTime
                value   13  unsigned int
            [14] Mon Dec 3 02:30:00 2018    14  
                key Mon Dec 3 02:30:00 2018 QDateTime
                value   14  unsigned int
            [15] Mon Dec 3 02:45:00 2018    15  
                key Mon Dec 3 02:45:00 2018 QDateTime
                value   15  unsigned int
            [16] Mon Dec 3 03:00:00 2018    16  
                key Mon Dec 3 03:00:00 2018 QDateTime
                value   16  unsigned int
            [17] Mon Dec 3 03:15:00 2018    17  
                key Mon Dec 3 03:15:00 2018 QDateTime
                value   17  unsigned int
            [18] Mon Dec 3 03:30:00 2018    18  
                key Mon Dec 3 03:30:00 2018 QDateTime
                value   18  unsigned int
            [19] Mon Dec 3 03:45:00 2018    19  
                key Mon Dec 3 03:45:00 2018 QDateTime
                value   19  unsigned int
            [20] Mon Dec 3 04:00:00 2018    20  
                key Mon Dec 3 04:00:00 2018 QDateTime
                value   20  unsigned int
             ....

Now, when I try to get the value which is associated with the key of "Sun Dec 2 23:00:00 2018", the code is returning me 14! but the correct value should be 0 clearly (based on the map values). This is the code I've used:

            quint32 mapValue = dateTime.value(date); // date is "Sun Dec 2 23:00:00 2018"

I'm coding qt for more than 8 years (but mostly Qt5) and this seems really strange. Maybe it's not correct way to define a map or maybe it's a Qt4.8 bug. I'll appreciate for any help. Thanks.

Edit: After debugging, I've found that the value associated with key#14 is equal with value associated with key#0 according to debugging information! Check out this: qt snapshot

In above image two values are detected equal.

Upvotes: 1

Views: 456

Answers (2)

Mosi
Mosi

Reputation: 1258

Just found the issue. It was due to dateTime specs. Some of them had different specs

Upvotes: 0

Aleph0
Aleph0

Reputation: 6074

I could not reproduce your problem. But one thing is for sure, that invalid dates are mapped to the same value, even though they might be initialized differently.

#include <QDateTime>
#include <QDebug>
#include <QMap>
int main(int argc, char** args) {
    QMap<QDateTime, quint32> map;

    map.clear();
    for (auto iter=0; iter< 10000; iter++)
    {
        QDateTime invalidDateTime;
        map[invalidDateTime] = iter;
    }
    qDebug() << map.size();
}

The following program prints 2 using Qt 5.13.0:

#include <QDateTime>
#include <QDebug>
#include <QMap>

int main(int argc, char** args) {
    QMap<QDateTime, quint32> map;

    QDateTime time1(QDate(2018, 12, 3), QTime(2, 30, 0));
    QDateTime time2(QDate(2018, 12, 3), QTime(23, 0, 0));

    map.insert(time1,0);
    map.insert(time2, 1);
    qDebug() << map.size();
}

Upvotes: 1

Related Questions