Important: Please read the Qt Code of Conduct -

[solved] Issues concatenate string with variables

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

  • Lifetime Qt Champion


    Do you mean:


    doesn't work ?

  • Moderators

    When you write
    implicitly a constructor "QString (const char * str) ": 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) ": 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>");
    don't forget to escape the quotes inside the string literal or use C++11 raw string literal if you can.

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

  • Moderators

    [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.

  • Maybe my fault .. gave me an error. I'll recheck.

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

Log in to reply