Answer the question
In order to leave comments, you need to log in
Why doesn't \n in QLabel wrap to a new line?
Here is the method that sets the text in the QLabel. Lines contain "\n". I replace it with "
" to move to a new line...
RegExp does not find "\n", but they are clearly there.
void setText(const std::string &text)
{
QString string(mTextCodec->toUnicode(text.c_str()));
string.replace(QRegExp("\\n"), QString("<br\>"));
mLabel.setText(string);
}
void setText(const std::string &text)
{
QString string("<i>I want to be able to switch it</i>\n<i>back if it doesn't fit.</i>");
string.replace(QRegExp("\\n"), QString("<br\>"));
mLabel.setText(string);
}
Answer the question
In order to leave comments, you need to log in
It was necessary to make 2nd screening:
1) Screening Sishnoe.
2) Escaping in RegExp.
Correct option:
string.replace(QRegExp("\\\\n"), QString("<br\>"));
RegExp does not find "\n", but they are clearly there.
...
\n is two characters in source code only, in executable code it is one character (with code 10).
\\n is three characters in the source code and two -- \n in the executable.
The QLabel manual has this example:QLabel *label = new QLabel(this); label->setFrameStyle(QFrame::Panel | QFrame::Sunken); label->setText("first line\nsecond line"); label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question