Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved I want check if one of my variables is null

    C++ Gurus
    4
    7
    7580
    Loading More Posts
    • 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.
    • A
      Armin last edited by kshegunov

      Hi,
      I want check if one of my variables is null
      some code will execute .
      This is my Code :

      if (ui->spinBox->text != &NULL )
      {
          {
          ary[counter]=price;
                 counter++;
          }
      ui->spinBox->clear();
      }
      }
      

      But i get this error:

      error: lvalue required as unary '&' operand
       if (ui->spinBox->text != &NULL )
                                 ^
      

      Why?
      Thanks.

      [Moved to C++ Gurus ~kshegunov]

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @Armin last edited by

        &NULL means nothing. You can't take the address of a constant, so the compiler is complaining about missing lvalue. Just use the implicit boolean interpretation:

        if (ui->spinBox->text)  {
            // It is not null
        }
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 1
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Because null doesn't have any address. It's not a pointer.

          QSpinBox::textreturns a QString. If you want to check if it's null then use QString::isNull.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 1
          • A
            Armin last edited by

            Thanks

            i use this code:

            QString s1 = "" ;

            if ((ui->spinBox->text) != s1 )
            
            

            and still get error

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Because text is a function. Why not use isNull or isEmpty ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 3
              • VRonin
                VRonin last edited by VRonin

                text is a method, not a member, if ((ui->spinBox->text()) != s1 ) yet again, as said above, the correct way would be if (ui->spinBox->text().isEmpty())

                "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 Reply Quote 3
                • A
                  Armin last edited by

                  Thanks for all
                  i solved my problem with this code :

                  if (ui->spinBox->text().isEmpty())

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post