Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QString check if it contains \n



  • 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?
    

  • Moderators

    @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


  • Lifetime Qt Champion

    @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?


  • Moderators

    @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



  • @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();                                                    
    
    

  • Moderators

    @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(); 
    


  • @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.



  • @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.


Log in to reply