How to add newline to a QString?
-
wrote on 8 Aug 2012, 22:43 last edited by
I have an error message dialog where I need to pass in a QString.
I want to break up the string into multiple lines. If I add "\n" it didn't work, it's simply ignored.
How can I break up the long string into several lines? TIA for your help. -
wrote on 8 Aug 2012, 23:35 last edited by
Well, \n should work, they should patch Qt on this one afaik ;)
But, as far as I remember, </br> could do the trick, as the contents in a QMessageBox is rendered as HTML like string.
-
wrote on 9 Aug 2012, 05:19 last edited by
Yes , "\n" works with QString , you can try the following code
@ QMessageBox::critical(this,"error","This \n is \n an \n error \n string !!!!!");@
that shows an error dialog like :
!http://img814.imageshack.us/img814/6962/capturelaa.png(error!!!)!
-
wrote on 11 Dec 2015, 17:35 last edited by
\n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
\n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text -
\n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
\n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain textwrote on 12 May 2022, 10:05 last edited by@BrianDavis said in How to add newline to a QString?:
\n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
\n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text\n works. Thanks @BrianDavis for sharing.
My code:QStringList strs; strs.append("Toyota"); strs.append("BUICK"); strs.append("VW"); strs.append("Mazda"); QFile p("car.txt"); if (p.open(QFile::WriteOnly | QIODevice::Text)) { QTextStream s(&p); for(QString line : strs){ s << line << "\n"; } }
-
@BrianDavis said in How to add newline to a QString?:
\n works when rendered as plain text, <br> works when rendered as html (textFormat: Text.StyledText)
\n is ignored when rendered as html, <br> is rendered as those 4 characters when rendered as plain text\n works. Thanks @BrianDavis for sharing.
My code:QStringList strs; strs.append("Toyota"); strs.append("BUICK"); strs.append("VW"); strs.append("Mazda"); QFile p("car.txt"); if (p.open(QFile::WriteOnly | QIODevice::Text)) { QTextStream s(&p); for(QString line : strs){ s << line << "\n"; } }
@Jimmy-Crab hi and welcome to devnet,
QStringList::join would be simpler.