Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Issues concatenate string with variables
Forum Updated to NodeBB v4.3 + New Features

[solved] Issues concatenate string with variables

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    McLion
    wrote on 28 May 2014, 14:49 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 May 2014, 19:50 last edited by
      #2

      Hi,

      Do you mean:

      @appendHtml(QString("<span>%1</span>").arg(message));@

      doesn't work ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 28 May 2014, 20:07 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          McLion
          wrote on 29 May 2014, 07:51 last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 29 May 2014, 09:54 last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              McLion
              wrote on 29 May 2014, 10:26 last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                McLion
                wrote on 30 May 2014, 09:37 last edited by
                #7

                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 Reply Last reply
                0

                1/7

                28 May 2014, 14:49

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved