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. Text_Changed
Forum Update on Monday, May 27th 2025

Text_Changed

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 728 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""

    void MainWindow::on_Value_textChanged(const QString &arg1)
    {
            if(arg1.toDouble()>30 || arg1.toDouble()<-30)
            {
         ui->textBrowser->setText("b");
               
    
         }
    
    
        else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30)
    
        {
          
            ui->Led->setPower(1);
    
      }
    //    //se la mia lineEdit è vuota setto i valori di dial e slider a zero
        else if (arg1 == "")
        {
    
          
            ui->textBrowser->setText("aa");
    

    }

    jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
    0
    • ? A Former User

      I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""

      void MainWindow::on_Value_textChanged(const QString &arg1)
      {
              if(arg1.toDouble()>30 || arg1.toDouble()<-30)
              {
           ui->textBrowser->setText("b");
                 
      
           }
      
      
          else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30)
      
          {
            
              ui->Led->setPower(1);
      
        }
      //    //se la mia lineEdit è vuota setto i valori di dial e slider a zero
          else if (arg1 == "")
          {
      
            
              ui->textBrowser->setText("aa");
      

      }

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @vale88 Try

      else if (arg1.isEmpty())
      

      instead.
      Also you should check first whether it is empty, because it does not make sense to convert an empty string to double.
      And you should do the conversion only once to make the code faster.

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

      1 Reply Last reply
      5
      • ? A Former User

        I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""

        void MainWindow::on_Value_textChanged(const QString &arg1)
        {
                if(arg1.toDouble()>30 || arg1.toDouble()<-30)
                {
             ui->textBrowser->setText("b");
                   
        
             }
        
        
            else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30)
        
            {
              
                ui->Led->setPower(1);
        
          }
        //    //se la mia lineEdit è vuota setto i valori di dial e slider a zero
            else if (arg1 == "")
            {
        
              
                ui->textBrowser->setText("aa");
        

        }

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @vale88 you're checking if the String converted to double is between -30 and 30. If the conversion fails, which is the case for an empty string, toDouble will return a 0. That is between -30 and 30


        edit example code correction:

         bool ok;
         double value = arg1.toDouble(&ok);
         if(!ok) 
           ui->textBrowser->setText("aa");
         else{
              if(value >30 || value <-30)  {
                    ui->textBrowser->setText("b");
              } else {
                    ui->Led->setPower(1);
              }
         }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        ? 1 Reply Last reply
        4
        • ? A Former User

          I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""

          void MainWindow::on_Value_textChanged(const QString &arg1)
          {
                  if(arg1.toDouble()>30 || arg1.toDouble()<-30)
                  {
               ui->textBrowser->setText("b");
                     
          
               }
          
          
              else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30)
          
              {
                
                  ui->Led->setPower(1);
          
            }
          //    //se la mia lineEdit è vuota setto i valori di dial e slider a zero
              else if (arg1 == "")
              {
          
                
                  ui->textBrowser->setText("aa");
          

          }

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @vale88 Be aware when using QString and "":

          QString empty;
          QString empty2("");
          
          empty == "" ==> is false
          empty2 == "" ==> is true
          emtpy == empty2 ==> is false!!
          

          I would first check if arg1 not empty, then if arg1 is a double value

          void MainWindow::on_Value_textChanged(const QString &arg1)
          {
              if(arg1.isEmpty())
              {
                    // ==> do something
                    return;
              }
              bool ok;
              double value = arg1.touDouble(&ok);
              if(!ok)
              {
                  // ==> arg1 not a double value...
                  return;
              }
           
              if(value >30 || value <-30)
              {
                  ui->textBrowser->setText("b");         
              }
              else
              {
                  ui->Led->setPower(1);
              }
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @vale88 you're checking if the String converted to double is between -30 and 30. If the conversion fails, which is the case for an empty string, toDouble will return a 0. That is between -30 and 30


            edit example code correction:

             bool ok;
             double value = arg1.toDouble(&ok);
             if(!ok) 
               ui->textBrowser->setText("aa");
             else{
                  if(value >30 || value <-30)  {
                        ui->textBrowser->setText("b");
                  } else {
                        ui->Led->setPower(1);
                  }
             }
            
            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @J.Hilk thenks..it works..I didn't undestand that when arg1=="" the result of conversion in 0

            KroMignonK 1 Reply Last reply
            0
            • ? A Former User

              @J.Hilk thenks..it works..I didn't undestand that when arg1=="" the result of conversion in 0

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @vale88 said in Text_Changed:

              I didn't undestand that when arg1=="" the result of conversion in 0

              It is the way how QString::toDouble() works ==> cf https://doc.qt.io/Qt-5/qstring.html#toDouble

              • Returns the string converted to a double value.
              • Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              ? 1 Reply Last reply
              3
              • KroMignonK KroMignon

                @vale88 said in Text_Changed:

                I didn't undestand that when arg1=="" the result of conversion in 0

                It is the way how QString::toDouble() works ==> cf https://doc.qt.io/Qt-5/qstring.html#toDouble

                • Returns the string converted to a double value.
                • Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).
                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @KroMignon thanks

                1 Reply Last reply
                0

                • Login

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