[solved] Issues concatenate string with variables
-
wrote on 28 May 2014, 14:49 last edited by
Hi
I'm trying to concatenate strings:
This works perfectly:
ui->debugMTS->appendHtml("<b><i>" + message + "</b></i>");This works not - I can't write it here since the editor is removing parts of it:
ui->debugMTS->appendHtml("<span >" + message + "</span>");
I also tried this to no avail:
message.prepend("<span >").arg(qFrontColor);
It actually is:
<span style='color:color..... but the forum editor removes it when I add the closing single quote.How can I add the color as a variable like Qt::red into a span style color string?
-
Hi,
Do you mean:
@appendHtml(QString("<span>%1</span>").arg(message));@
doesn't work ?
-
When you write
@funcTakesQString("bar");@
implicitly a constructor "QString (const char * str) ":http://qt-project.org/doc/qt-5/qstring.html#QString-8 is called and the QString argument is passed.
When you write
@QString foo;
funcTakesQString("bar" + foo)
@
a "const QString operator+ (const char * s1, const QString & s2) ":http://qt-project.org/doc/qt-5/qstring.html#operator-2b-35 is called and resulting QString is passed in.
When you write
@int foo;
funcTakesQString("bar" + foo)
@
the "bar" + foo is pointer arithmetic (not string concatenation) and the result is const char*. funcTakesQString doesn't have an overload for const char* so you get an error.The solution here is to create the message as QString in the first place and also use the number converted to QString representation(there is no automatic conversion of numbers to their string representation in c++).
The method arg() accepts a QString and you're passing it a QColor. There is no automatic conversion of QColor to QString but it has a method that does that: name().So something like this will work:
@
QColor mycolor = ...; //whatever
QString mymessage("<span style="color: %1">My message</span>");
ui->debugMTS->appendHtml(mymessage.arg(mycolor.name()));
@
don't forget to escape the quotes inside the string literal or use C++11 raw string literal if you can. -
wrote on 29 May 2014, 07:51 last edited by
Thanks. You cleared up some things for me.
Unfortunately, the name() method returns the color as "#RRGGBB" and not its name like "red". I solved it with some own defines for the color-names instead of using the predefined ones and this changes:
@void QTGUI_MainWindow::DebugShow(const QString Message, uint iStyle, QString FrontColor, QString BackColor)
....
ui->debugMTS->appendHtml(QString("<span style='color:%1'>%2</span>").arg(FrontColor, Message));@Thank you guys.
McL -
[quote author="McLion" date="1401349908"]Unfortunately, the name() method returns the color as "#RRGGBB" and not its name like "red".[/quote]
Why is that a bad thing? That's how you define colors in css(well, one of the ways at least). It doesn't matter if it's "red" or "#FF0000" or "rgb(255,0,0)". It's still the same, valid css color definition. -
wrote on 29 May 2014, 10:26 last edited by
Maybe my fault .. gave me an error. I'll recheck.
Thanks. -
wrote on 30 May 2014, 09:37 last edited by
I must have been doing something wrong the first time. Works perfectly now.
Thanks for the heads-up.@void QTGUI_MainWindow::DebugShow(const QString Message, uint iStyle, QColor FrontColor, QColor BackColor)
...
ui->debugMTS->appendHtml(QString("<span style='color:%1;background:%2'>%3</span>").arg(FrontColor.name(), BackColor.name(), Message));@
1/7