Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. Qt Contribution
  4. QStringPaint library released :)
QtWS25 Last Chance

QStringPaint library released :)

Scheduled Pinned Locked Moved Qt Contribution
qstringpaintqstringpaintcolorifystringlibrary
4 Posts 3 Posters 2.9k Views
  • 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.
  • A Offline
    A Offline
    Arty.McLabin
    wrote on 24 Jul 2015, 18:45 last edited by koahnig
    #1

    Hi folks, i've made a library for fast transforming "raw" QStrings into colored by css/html.
    https://github.com/ArtyMcLabin/QStringPaint
    most primitive (and needed) usage example:

    #include "QStringPaint"
      
       QString resultMessage;
    if(foo()){
    resultMessage = "a green success!";
    ui->label1->setText(QStringPaint(resultMessage,Qt::green));
    }
    
    else
    resultMessage = "a red error :(";
    ui->label1->setText(QStringPaint(resultMessage,"#ff0000"));
    

    hope some of you will find it useful as i do :>

    Static linking is cool. Really.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 24 Jul 2015, 19:53 last edited by
      #2

      Thanks for sharing.

      I have added some code tags to your post.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 25 Jul 2015, 07:26 last edited by Chris Kawa
        #3

        Thanks for sharing.

        Couple of suggestions though:

        • For performance reasons avoid passing parameters by values or pointers. This is true even with implicitly shared types like QString. Passing it by value creates copies and is extremely bad for locality. Passing pointers is optimizer unfriendly. Always prefer a const reference if it's an "in" parameter or a non-const reference if it's an "in-out" parameter. Also avoid in-out parameters if possible and prefer returning a value. If you're interested in more details I recommend this excellent talk.

        • Again, for performance reasons concatenating strings like this is bad: QString a = "foo" + someString + "bar". Prefer creating a string, calling reserve with calculated length and then concatenating QLatin1String. A little worse, but also better than what you have is to create the string with placeholders and using the arg() method, which does more or less the above.

        • instead of taking an int parameter with a large switch case give it a const QColor& param and use its name() method to convert it to css compatible string. Since QColor has a constructor overloads for various types this would work with various parameters like QColor::fromRgb(128,128,128), Qt::red, 0xFF6633 etc.

        • Just a stylistic note: QSomeName for Qt users suggests a class name. Lowercase is preferred for function names in Qt api. Since it's not part of official Qt package I would also avoid the Q name entirely.

        So an optimized interface for this would look like this:

        QString stringPaint(const QString& str, const QString& color);
        QString stringPaint(const QString& str, const QColor& color);
        

        I would also consider adding an overload for const char* and const QLatin1String&.

        A 1 Reply Last reply 27 Aug 2015, 08:51
        2
        • C Chris Kawa
          25 Jul 2015, 07:26

          Thanks for sharing.

          Couple of suggestions though:

          • For performance reasons avoid passing parameters by values or pointers. This is true even with implicitly shared types like QString. Passing it by value creates copies and is extremely bad for locality. Passing pointers is optimizer unfriendly. Always prefer a const reference if it's an "in" parameter or a non-const reference if it's an "in-out" parameter. Also avoid in-out parameters if possible and prefer returning a value. If you're interested in more details I recommend this excellent talk.

          • Again, for performance reasons concatenating strings like this is bad: QString a = "foo" + someString + "bar". Prefer creating a string, calling reserve with calculated length and then concatenating QLatin1String. A little worse, but also better than what you have is to create the string with placeholders and using the arg() method, which does more or less the above.

          • instead of taking an int parameter with a large switch case give it a const QColor& param and use its name() method to convert it to css compatible string. Since QColor has a constructor overloads for various types this would work with various parameters like QColor::fromRgb(128,128,128), Qt::red, 0xFF6633 etc.

          • Just a stylistic note: QSomeName for Qt users suggests a class name. Lowercase is preferred for function names in Qt api. Since it's not part of official Qt package I would also avoid the Q name entirely.

          So an optimized interface for this would look like this:

          QString stringPaint(const QString& str, const QString& color);
          QString stringPaint(const QString& str, const QColor& color);
          

          I would also consider adding an overload for const char* and const QLatin1String&.

          A Offline
          A Offline
          Arty.McLabin
          wrote on 27 Aug 2015, 08:51 last edited by
          #4

          @Chris-Kawa said:

          t by value creates copies and is extremely bad for locality. Passing pointers is optimizer unfriendly. Always prefer a const referenc

          thanks for the advices! i will start working on it.

          Static linking is cool. Really.

          1 Reply Last reply
          0

          • Login

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