Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't get QMap value using a <double> key
Forum Update on Monday, May 27th 2025

Can't get QMap value using a <double> key

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 305 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Atr0p0sA Offline
    Atr0p0sA Offline
    Atr0p0s
    wrote on last edited by
    #1

    I'm facing the problem that I can't get the value of a QMap using a key other than using a QMapIterator key.

    I have such QMap:
    QMap<double, QList<double> > dynamic_Data;

    And as you can see I can't get value using 133040 as a key.

    QMapIterator<double, QList<double> > i(dynamic_Data);
    if (i.hasNext()) {
        i.next();
        qDebug() << i.key() << ":" << i.value();
        qDebug() << dynamic_Data.value(i.key());
        qDebug() << dynamic_Data.value(double(133040));
    }
    

    Output:

    133040 : (31.8689, 49.2064)
    (31.8689, 49.2064)
    ()
    

    Maybe I don't know something about QMap or I'm doing something wrong?

    JonBJ 1 Reply Last reply
    0
    • Atr0p0sA Atr0p0s

      I'm facing the problem that I can't get the value of a QMap using a key other than using a QMapIterator key.

      I have such QMap:
      QMap<double, QList<double> > dynamic_Data;

      And as you can see I can't get value using 133040 as a key.

      QMapIterator<double, QList<double> > i(dynamic_Data);
      if (i.hasNext()) {
          i.next();
          qDebug() << i.key() << ":" << i.value();
          qDebug() << dynamic_Data.value(i.key());
          qDebug() << dynamic_Data.value(double(133040));
      }
      

      Output:

      133040 : (31.8689, 49.2064)
      (31.8689, 49.2064)
      ()
      

      Maybe I don't know something about QMap or I'm doing something wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Atr0p0s
      How do you know that i.key() == double(133040)? Please print out the value of that expression. qDebug() << i.key() => 133040 is not necessarily accurate enough to tell you.

      1 Reply Last reply
      3
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        I'm guessing that @JonB is correct.

        You can also try:

        qDebug() << Qt::fixed << qSetRealNumberPrecision(20) << i.key();
        

        Cheers.

        Atr0p0sA 1 Reply Last reply
        2
        • Atr0p0sA Atr0p0s has marked this topic as solved on
        • Paul ColbyP Paul Colby

          I'm guessing that @JonB is correct.

          You can also try:

          qDebug() << Qt::fixed << qSetRealNumberPrecision(20) << i.key();
          

          Cheers.

          Atr0p0sA Offline
          Atr0p0sA Offline
          Atr0p0s
          wrote on last edited by
          #4

          @Paul-Colby @JonB you are right, the key isn't exactly 133040. I guess it's better not to use double type for keys. Thank you, men.

          JonBJ 1 Reply Last reply
          1
          • Atr0p0sA Atr0p0s

            @Paul-Colby @JonB you are right, the key isn't exactly 133040. I guess it's better not to use double type for keys. Thank you, men.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Atr0p0s said in Can't get QMap value using a <double> key:

            I guess it's better not to use double type for keys

            :) You have to be careful with doubles. As a great author once said [paraphrased]: "All [well, some] doubles are equal, and some doubles are more equal than others" ;-)

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved