The use of Chinese characters in Qt and the solution to the “error C2001: newline in constant” problem

Problem description: In my program, I want to use the following code to change the prompt in the input field to “Please enter your password”. But the program error, unable to recognize Chinese characters.

QLineEdit *passwordEdit;
passwordEdit->setPlaceholderText("Enter your password please");

solution: use QString::fromLocal8Bit() method to transcode Chinese characters, the program can normally recognize Chinese character QString. Change the above code as follows:

    QString str;
    str=str.fromLocal8Bit("Enter your password please");
    passwordEdit->setPlaceholderText(str);

another problem: program error “error C2001: newline in constant”


Problem analysis: The encoding mode of the code file Login. CPP is not correct. Changing the encoding mode of the file login. CPP to UTF-8 can solve this problem. The method I used to modify the encoding method of the file was to open the CPP file with notepad software and select the encoding form of the file in the “Save as” window:

After saving the file as UTF-8, reload the file and compile the program. The error disappears and the Chinese character can be displayed normally.

Read More: