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. Is there a "new" style for casting?
Forum Updated to NodeBB v4.3 + New Features

Is there a "new" style for casting?

Scheduled Pinned Locked Moved Solved General and Desktop
46 Posts 9 Posters 32.9k Views 3 Watching
  • 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.
  • jsulmJ jsulm

    @saber said in Is there a "new" style for casting?:

    i got more warnings to fix.

    So, fix them

    S Offline
    S Offline
    saber
    wrote on last edited by
    #26

    @jsulm
    sorry .
    i don't think those are casting.and i am not understanding what is the fix.

    jsulmJ 1 Reply Last reply
    0
    • S saber

      @jsulm
      sorry .
      i don't think those are casting.and i am not understanding what is the fix.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #27

      @saber The first warning says already what the problem is: comparing floating point numbers with == and != isn't save because of inaccuracy of such numbers represented in a computer. Depending on what you really want you should cast to int or use <, > for comparisions.
      For the second warning take a look at https://softwareengineering.stackexchange.com/questions/179269/why-does-clang-llvm-warn-me-about-using-default-in-a-switch-statement-where-all
      In general, you should try to find out what the problem is by yourself first before going to a forum and asking. Or do you want to ask for each and every warning you get? I mean, I'm happy to help, but sometimes I have the impression that people don't even try to find an answer by themselves.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • jsulmJ jsulm

        @saber The first warning says already what the problem is: comparing floating point numbers with == and != isn't save because of inaccuracy of such numbers represented in a computer. Depending on what you really want you should cast to int or use <, > for comparisions.
        For the second warning take a look at https://softwareengineering.stackexchange.com/questions/179269/why-does-clang-llvm-warn-me-about-using-default-in-a-switch-statement-where-all
        In general, you should try to find out what the problem is by yourself first before going to a forum and asking. Or do you want to ask for each and every warning you get? I mean, I'm happy to help, but sometimes I have the impression that people don't even try to find an answer by themselves.

        S Offline
        S Offline
        saber
        wrote on last edited by
        #28

        @jsulm
        i understand all the warnings but problem is i don't know how to fix those .

        i know this issues are small and silly to ask to help.but i have no other way. my app's co-developer is in off-line. i am a ui designer and newbie c++ developer .

        thanks.

        jsulmJ 1 Reply Last reply
        0
        • S saber

          @jsulm
          i understand all the warnings but problem is i don't know how to fix those .

          i know this issues are small and silly to ask to help.but i have no other way. my app's co-developer is in off-line. i am a ui designer and newbie c++ developer .

          thanks.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #29

          @saber I already suggested what to change and provided a link. And you can round the floating point numbers to int to get rid of this message. And you can use Google.
          Also you should think about whether you need floating point numbers at all...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          0
          • jsulmJ jsulm

            @saber I already suggested what to change and provided a link. And you can round the floating point numbers to int to get rid of this message. And you can use Google.
            Also you should think about whether you need floating point numbers at all...

            S Offline
            S Offline
            saber
            wrote on last edited by
            #30

            @jsulm
            yes , it seems fixed.

                switch( m_model->state() ) {
                    case Battery::FullyCharged:
                        ui->statusEdit->setText( tr( "Full Charged" ) );
                        ui->timerLblEdit->setText( tr( "Full Charged" ) );
                        break;
                    case Battery::Discharging:
                        addSeconds = static_cast<int>(m_model->toEmpty());
                        addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energy - energyEmpty ) * 4 / rate) : addSeconds;
                        ui->statusEdit->setText( tr( "Discharging" ) );
                        ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                        break;
                    case Battery::Charging:
                        addSeconds = static_cast<int>(m_model->toFull());
                        addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energyFull - energy ) * 4 / rate) : addSeconds;
                        ui->statusEdit->setText( tr( "Charging" ) );
                        ui->timerLblEdit->setText( tr( "Charged in : " ) );
                        break;
                    default:
                        ui->statusEdit->setText( tr( "No Battery" ) );
                        ui->timerLblEdit->setText( tr( "No Battery" ) );
                        break;
                }
            

            as for the default , i read the post and understand that i can't do anything as the library i am using there is no eum like "no battery" . so it needs the default case.

            jsulmJ 1 Reply Last reply
            0
            • S saber

              @jsulm
              yes , it seems fixed.

                  switch( m_model->state() ) {
                      case Battery::FullyCharged:
                          ui->statusEdit->setText( tr( "Full Charged" ) );
                          ui->timerLblEdit->setText( tr( "Full Charged" ) );
                          break;
                      case Battery::Discharging:
                          addSeconds = static_cast<int>(m_model->toEmpty());
                          addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energy - energyEmpty ) * 4 / rate) : addSeconds;
                          ui->statusEdit->setText( tr( "Discharging" ) );
                          ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                          break;
                      case Battery::Charging:
                          addSeconds = static_cast<int>(m_model->toFull());
                          addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energyFull - energy ) * 4 / rate) : addSeconds;
                          ui->statusEdit->setText( tr( "Charging" ) );
                          ui->timerLblEdit->setText( tr( "Charged in : " ) );
                          break;
                      default:
                          ui->statusEdit->setText( tr( "No Battery" ) );
                          ui->timerLblEdit->setText( tr( "No Battery" ) );
                          break;
                  }
              

              as for the default , i read the post and understand that i can't do anything as the library i am using there is no eum like "no battery" . so it needs the default case.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #31

              @saber You can: remove the default. If then a new value is added to the enum you will get a warning from compiler that this new value is not handled.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                saber
                wrote on last edited by saber
                #32

                for now i will leave as it is and inform the library author to add that enm.

                i got more warning to fix.

                0_1534231505882_aw.png

                i am getting this in all my ui based header file .
                but the app compiles fine.

                sierdzioS JonBJ jsulmJ 3 Replies Last reply
                0
                • S saber

                  for now i will leave as it is and inform the library author to add that enm.

                  i got more warning to fix.

                  0_1534231505882_aw.png

                  i am getting this in all my ui based header file .
                  but the app compiles fine.

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #33

                  @saber said in Is there a "new" style for casting?:

                  for now i will leave as it is and inform the library author to add that enm.

                  i got more warning to fix.

                  0_1534231505882_aw.png

                  i am getting this in all my ui based header file .
                  but the app compiles fine.

                  Ignore it, then. The code model sometimes gets bogged down in ifdefs without reason.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • S saber

                    for now i will leave as it is and inform the library author to add that enm.

                    i got more warning to fix.

                    0_1534231505882_aw.png

                    i am getting this in all my ui based header file .
                    but the app compiles fine.

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

                    @saber
                    Although we cannot be sure, chances are that the last line of that header file should be:

                    #endif
                    

                    to match the #ifndef started at line #17. (This is a common pattern to allow a header to be included more than once without it doing any harm.) Have a look at other header files (maybe from the same library?) and you should see this. You should fix this by adding the line --- at present the compiler/editor will be treating your file as though it did have that line at the end.

                    sierdzioS S 2 Replies Last reply
                    0
                    • JonBJ JonB

                      @saber
                      Although we cannot be sure, chances are that the last line of that header file should be:

                      #endif
                      

                      to match the #ifndef started at line #17. (This is a common pattern to allow a header to be included more than once without it doing any harm.) Have a look at other header files (maybe from the same library?) and you should see this. You should fix this by adding the line --- at present the compiler/editor will be treating your file as though it did have that line at the end.

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #35

                      @JonB said in Is there a "new" style for casting?:

                      at present the compiler/editor will be treating your file as though it did have that line at the end.

                      Whoa, really? Which compiler allows that?

                      For me (GCC) an unterminated ifdef is a hard error, compiler does not implicitly add it.

                      (Z(:^

                      JonBJ 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @saber
                        Although we cannot be sure, chances are that the last line of that header file should be:

                        #endif
                        

                        to match the #ifndef started at line #17. (This is a common pattern to allow a header to be included more than once without it doing any harm.) Have a look at other header files (maybe from the same library?) and you should see this. You should fix this by adding the line --- at present the compiler/editor will be treating your file as though it did have that line at the end.

                        S Offline
                        S Offline
                        saber
                        wrote on last edited by
                        #36

                        @JonB
                        on every file "#endif " is added from first .
                        all the warnings was coming up after the update of qtcreator.

                        see this
                        on the old version qt created this by it self

                        explicit MainWindow(QWidget *parent = 0);
                        

                        after update
                        0_1534232269589_y.png

                        should i replace the 0 with nullpointer on every file??
                        should it be a error in older version of compiler??
                        in my qmake i add that must have c++ 11 support .

                        sierdzioS 1 Reply Last reply
                        0
                        • S saber

                          @JonB
                          on every file "#endif " is added from first .
                          all the warnings was coming up after the update of qtcreator.

                          see this
                          on the old version qt created this by it self

                          explicit MainWindow(QWidget *parent = 0);
                          

                          after update
                          0_1534232269589_y.png

                          should i replace the 0 with nullpointer on every file??
                          should it be a error in older version of compiler??
                          in my qmake i add that must have c++ 11 support .

                          sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #37

                          @saber said in Is there a "new" style for casting?:

                          @JonB
                          on every file "#endif " is added from first .
                          all the warnings was coming up after the update of qtcreator.

                          see this
                          on the old version qt created this by it self

                          explicit MainWindow(QWidget *parent = 0);
                          

                          after update
                          0_1534232269589_y.png

                          should i replace the 0 with nullpointer on every file??
                          should it be a error in older version of compiler??
                          in my qmake i add that must have c++ 11 support .

                          These are hints from clang on how to improve your code. You are not forced to do everything it says. You are seeing them now because the clang code model is default since Qt Creator 4.7. So previously you were using the old code model which did not offer these hints. That's all there is to it. If the code compiles fine (without warnings), you can ignore the hints.

                          However, it is a good idea to at least consider them, because these hints are (in 90% of cases) very good hints indeed.

                          In this case yes, I'd strongly recommend using nullptr rather than zero. It is an explicit, named value that makes it harder to make a mistake.

                          (Z(:^

                          S 1 Reply Last reply
                          4
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #38

                            I'm late to the party but just wanted to note Qt provides qFuzzyCompare and qFuzzyIsNull to compare double numbers for equality but you have to read the docs carefully before using them

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            2
                            • sierdzioS sierdzio

                              @saber said in Is there a "new" style for casting?:

                              @JonB
                              on every file "#endif " is added from first .
                              all the warnings was coming up after the update of qtcreator.

                              see this
                              on the old version qt created this by it self

                              explicit MainWindow(QWidget *parent = 0);
                              

                              after update
                              0_1534232269589_y.png

                              should i replace the 0 with nullpointer on every file??
                              should it be a error in older version of compiler??
                              in my qmake i add that must have c++ 11 support .

                              These are hints from clang on how to improve your code. You are not forced to do everything it says. You are seeing them now because the clang code model is default since Qt Creator 4.7. So previously you were using the old code model which did not offer these hints. That's all there is to it. If the code compiles fine (without warnings), you can ignore the hints.

                              However, it is a good idea to at least consider them, because these hints are (in 90% of cases) very good hints indeed.

                              In this case yes, I'd strongly recommend using nullptr rather than zero. It is an explicit, named value that makes it harder to make a mistake.

                              S Offline
                              S Offline
                              saber
                              wrote on last edited by
                              #39

                              @sierdzio
                              i am adding all the fix that qtcreator suggesting .

                              as for the " #endif " i am ignoring it.

                              aha_1980A 1 Reply Last reply
                              0
                              • sierdzioS sierdzio

                                @JonB said in Is there a "new" style for casting?:

                                at present the compiler/editor will be treating your file as though it did have that line at the end.

                                Whoa, really? Which compiler allows that?

                                For me (GCC) an unterminated ifdef is a hard error, compiler does not implicitly add it.

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

                                @sierdzio said in Is there a "new" style for casting?:

                                @JonB said in Is there a "new" style for casting?:

                                at present the compiler/editor will be treating your file as though it did have that line at the end.

                                Whoa, really? Which compiler allows that?
                                For me (GCC) an unterminated ifdef is a hard error, compiler does not implicitly add it.

                                The kind which generates a screenshot "Unterminated #ifdef" shown by the OP who says it's a warning but the app compiles fine.

                                Now that you have said

                                Ignore it, then. The code model sometimes gets bogged down in ifdefs without reason.

                                I knew nothing about it being "clang". I took it that the OP had perhaps failed to get the last line of whatever he was fetching/copying (stranger things have been known to happen on this forum...). The OP should ignore my suggestion and stick with yours!

                                1 Reply Last reply
                                0
                                • S saber

                                  for now i will leave as it is and inform the library author to add that enm.

                                  i got more warning to fix.

                                  0_1534231505882_aw.png

                                  i am getting this in all my ui based header file .
                                  but the app compiles fine.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #41

                                  @saber said in Is there a "new" style for casting?:

                                  add that enm

                                  Please don't there is NOTHING to add to the enum! Simply remove the "default" inside the switch. Please read the link I posted more carefully, it is explained there.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  S 1 Reply Last reply
                                  3
                                  • jsulmJ jsulm

                                    @saber said in Is there a "new" style for casting?:

                                    add that enm

                                    Please don't there is NOTHING to add to the enum! Simply remove the "default" inside the switch. Please read the link I posted more carefully, it is explained there.

                                    S Offline
                                    S Offline
                                    saber
                                    wrote on last edited by
                                    #42

                                    @jsulm
                                    ok.

                                    1 Reply Last reply
                                    0
                                    • S saber

                                      @sierdzio
                                      i am adding all the fix that qtcreator suggesting .

                                      as for the " #endif " i am ignoring it.

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #43

                                      Hi @saber,

                                      the "Unterminated conditional directive" could be either QTCREATORBUG-18801 or QTCREATORBUG-20883

                                      Regards,

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      1
                                      • S Offline
                                        S Offline
                                        saber
                                        wrote on last edited by
                                        #44

                                        i found new issue

                                        QString DiskInfo::getDiskName() const
                                        {
                                            QDir blocks("/sys/block");
                                        
                                            for (const QFileInfo entryInfo : blocks.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
                                                if (QFile::exists(QString("%1/device").arg(entryInfo.absoluteFilePath()))) {
                                                    return entryInfo.baseName();
                                                }
                                            }
                                            return QString();
                                        }
                                        

                                        here qt telling me

                                        warning: loop variable 'entryInfo' of type 'const QFileInfo' creates a copy from type 'const QFileInfo'
                                        
                                        A 1 Reply Last reply
                                        0
                                        • S saber

                                          i found new issue

                                          QString DiskInfo::getDiskName() const
                                          {
                                              QDir blocks("/sys/block");
                                          
                                              for (const QFileInfo entryInfo : blocks.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
                                                  if (QFile::exists(QString("%1/device").arg(entryInfo.absoluteFilePath()))) {
                                                      return entryInfo.baseName();
                                                  }
                                              }
                                              return QString();
                                          }
                                          

                                          here qt telling me

                                          warning: loop variable 'entryInfo' of type 'const QFileInfo' creates a copy from type 'const QFileInfo'
                                          
                                          A Offline
                                          A Offline
                                          Asperamanca
                                          wrote on last edited by
                                          #45

                                          @saber
                                          In general, when you have a new question, post in a new thread. Since this thread is marked as "solved", people may not look.

                                          To your question: You loop through a list of QFileInfo objects. Your code copies the QFileInfo for every loop, which is unnecessary. Instead, you can use a const reference:

                                          for (const QFileInfo& entryInfo : blocks.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot))
                                          
                                          S 1 Reply Last reply
                                          2

                                          • Login

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