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. QString check if it contains \n
QtWS25 Last Chance

QString check if it contains \n

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 4.4k 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on 14 Nov 2019, 10:02 last edited by
    #1

    Hi,

    For my application I am using a QInputDialog, but I want to check if the input string contains \n.
    Also I would like to create a message box saying that the combination \n is not allowed as input.

    I already used :

    QString input = QInputDialog...........
    if(input.contains("\n")
    {
         QMessagebox........
    }
    
    First thing:
    It does not seem to check properly if \n was in the string.
    
    Second: 
    I can not display the message, because when I try to put \n in the messagebox it just creates a new line
    
    
    Is there anyone who has done this before who can help me out?
    
    J J 2 Replies Last reply 14 Nov 2019, 10:04
    0
    • H hobbyProgrammer
      14 Nov 2019, 10:02

      Hi,

      For my application I am using a QInputDialog, but I want to check if the input string contains \n.
      Also I would like to create a message box saying that the combination \n is not allowed as input.

      I already used :

      QString input = QInputDialog...........
      if(input.contains("\n")
      {
           QMessagebox........
      }
      
      First thing:
      It does not seem to check properly if \n was in the string.
      
      Second: 
      I can not display the message, because when I try to put \n in the messagebox it just creates a new line
      
      
      Is there anyone who has done this before who can help me out?
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 14 Nov 2019, 10:08 last edited by J.Hilk
      #3

      @hobbyProgrammer
      it contains should work, but you could also try it this way:

      (s.contains(QChar::LineFeed) || s.contains(QChar::CarriageReturn)
      

      To display \ in a textfield, you have to escape it with an additional \ -> \\n


      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.

      H 1 Reply Last reply 14 Nov 2019, 10:12
      5
      • H hobbyProgrammer
        14 Nov 2019, 10:02

        Hi,

        For my application I am using a QInputDialog, but I want to check if the input string contains \n.
        Also I would like to create a message box saying that the combination \n is not allowed as input.

        I already used :

        QString input = QInputDialog...........
        if(input.contains("\n")
        {
             QMessagebox........
        }
        
        First thing:
        It does not seem to check properly if \n was in the string.
        
        Second: 
        I can not display the message, because when I try to put \n in the messagebox it just creates a new line
        
        
        Is there anyone who has done this before who can help me out?
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 14 Nov 2019, 10:04 last edited by
        #2

        @hobbyProgrammer said in QString check if it contains \n:

        I can not display the message, because when I try to put \n in the messagebox it just creates a new line

        That's what \n does - it creates new line. What display message do you want to display?

        "It does not seem to check properly if \n was in the string." - does the string contain \n?

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

        1 Reply Last reply
        1
        • H hobbyProgrammer
          14 Nov 2019, 10:02

          Hi,

          For my application I am using a QInputDialog, but I want to check if the input string contains \n.
          Also I would like to create a message box saying that the combination \n is not allowed as input.

          I already used :

          QString input = QInputDialog...........
          if(input.contains("\n")
          {
               QMessagebox........
          }
          
          First thing:
          It does not seem to check properly if \n was in the string.
          
          Second: 
          I can not display the message, because when I try to put \n in the messagebox it just creates a new line
          
          
          Is there anyone who has done this before who can help me out?
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 14 Nov 2019, 10:08 last edited by J.Hilk
          #3

          @hobbyProgrammer
          it contains should work, but you could also try it this way:

          (s.contains(QChar::LineFeed) || s.contains(QChar::CarriageReturn)
          

          To display \ in a textfield, you have to escape it with an additional \ -> \\n


          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.

          H 1 Reply Last reply 14 Nov 2019, 10:12
          5
          • J J.Hilk
            14 Nov 2019, 10:08

            @hobbyProgrammer
            it contains should work, but you could also try it this way:

            (s.contains(QChar::LineFeed) || s.contains(QChar::CarriageReturn)
            

            To display \ in a textfield, you have to escape it with an additional \ -> \\n

            H Offline
            H Offline
            hobbyProgrammer
            wrote on 14 Nov 2019, 10:12 last edited by hobbyProgrammer
            #4

            @J-Hilk thanks!

            That worked well for me.

                bool ok;
            
                QInputDialog *dialog = new QInputDialog();  
                QString text = dialog->getText(this, tr("input dialog"), tr("Please enter input:"), QLineEdit::Normal, "", &ok);       
            
                    QMessageBox warning;                                                   
                    warning.setIcon(QMessageBox::Critical);                            
                    if(text == "")                                                          
                    {
                        warning.setText("The input can not be empty");        
            
                    }
                    else if(text.contains(",") || text.contains("\\n") )                     
                    {
                        warning.setText("The input can not contain a comma or '\\n'");       // this displays as "The input can not contain a comma or '\n' "
                    }
                    warning.exec();                                                    
            
            
            J JonBJ 2 Replies Last reply 14 Nov 2019, 10:21
            0
            • H hobbyProgrammer
              14 Nov 2019, 10:12

              @J-Hilk thanks!

              That worked well for me.

                  bool ok;
              
                  QInputDialog *dialog = new QInputDialog();  
                  QString text = dialog->getText(this, tr("input dialog"), tr("Please enter input:"), QLineEdit::Normal, "", &ok);       
              
                      QMessageBox warning;                                                   
                      warning.setIcon(QMessageBox::Critical);                            
                      if(text == "")                                                          
                      {
                          warning.setText("The input can not be empty");        
              
                      }
                      else if(text.contains(",") || text.contains("\\n") )                     
                      {
                          warning.setText("The input can not contain a comma or '\\n'");       // this displays as "The input can not contain a comma or '\n' "
                      }
                      warning.exec();                                                    
              
              
              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 14 Nov 2019, 10:21 last edited by J.Hilk
              #5

              @hobbyProgrammer
              great,
              If I may propose a view cosmetic changes :)

                  QMessageBox warning;                                                   
                  warning.setIcon(QMessageBox::Critical);                            
                  if(text.isEmpty())                                                          
                  {
                      warning.setText("The input can not be empty");        
                      
                  }
                  else if(text.contains(",") || text.contains(QChar::LineFeed) )                     
                  {
                      warning.setText("The input can not contain a comma or '\\n'");       // this displays as "The input can not contain a comma or '\n' "
                  }
                  warning.exec(); 
              

              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
              0
              • H hobbyProgrammer
                14 Nov 2019, 10:12

                @J-Hilk thanks!

                That worked well for me.

                    bool ok;
                
                    QInputDialog *dialog = new QInputDialog();  
                    QString text = dialog->getText(this, tr("input dialog"), tr("Please enter input:"), QLineEdit::Normal, "", &ok);       
                
                        QMessageBox warning;                                                   
                        warning.setIcon(QMessageBox::Critical);                            
                        if(text == "")                                                          
                        {
                            warning.setText("The input can not be empty");        
                
                        }
                        else if(text.contains(",") || text.contains("\\n") )                     
                        {
                            warning.setText("The input can not contain a comma or '\\n'");       // this displays as "The input can not contain a comma or '\n' "
                        }
                        warning.exec();                                                    
                
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 14 Nov 2019, 10:28 last edited by JonB
                #6

                @hobbyProgrammer
                Your approach is fine in that it will work. However, the user interface is such that the user is allowed to type in unacceptable stuff and still press OK to exit the dialog. When you then detect that, you will have to put the user back into the dialog to start over.

                A nicer approach is to validate the input while inside the dialog, and warn and keep the user there till they give you something acceptable, don't you think? I haven't looked at QInputDialog deeply, but perhaps overriding its https://doc.qt.io/qt-5/qinputdialog.html#done and/or handling https://doc.qt.io/qt-5/qinputdialog.html#textValueChanged can be used to achieve that.

                H 1 Reply Last reply 14 Nov 2019, 10:33
                3
                • JonBJ JonB
                  14 Nov 2019, 10:28

                  @hobbyProgrammer
                  Your approach is fine in that it will work. However, the user interface is such that the user is allowed to type in unacceptable stuff and still press OK to exit the dialog. When you then detect that, you will have to put the user back into the dialog to start over.

                  A nicer approach is to validate the input while inside the dialog, and warn and keep the user there till they give you something acceptable, don't you think? I haven't looked at QInputDialog deeply, but perhaps overriding its https://doc.qt.io/qt-5/qinputdialog.html#done and/or handling https://doc.qt.io/qt-5/qinputdialog.html#textValueChanged can be used to achieve that.

                  H Offline
                  H Offline
                  hobbyProgrammer
                  wrote on 14 Nov 2019, 10:33 last edited by
                  #7

                  @JonB
                  Hi,
                  Thank you so much for your feedback!
                  I am quite new to programming and always looking for ways to improve.
                  This will most certainly help.

                  1 Reply Last reply
                  0

                  1/7

                  14 Nov 2019, 10:02

                  • Login

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