Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Create QIcon from a string

Create QIcon from a string

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 4 Posters 1.7k Views 4 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.
  • P Offline
    P Offline
    peter-70
    wrote on last edited by peter-70
    #1

    I have a piece of code to create an QIcon from given string:

    QIcon* CreateIcon(const QString& content, int iw, int ih)
    {
        auto pixmap = new QPixmap(iw, ih);
        auto painter = new QPainter(pixmap);
        painter->setPen(QColor(255, 0, 0, 128));
        painter->drawText(QRect(0, 0, iw, ih), content);
        return new QIcon(*pixmap);
    }
    

    Unfortunately the icon will be drawn and I can see the text, but somehow impure.
    What I do wrong?
    Probable the code is incomplete...

    Thanks for help!

    1 Reply Last reply
    0
    • CKurduC Offline
      CKurduC Offline
      CKurdu
      wrote on last edited by
      #2

      Maybe system using bitmap font. Set a scalable font using by void QPainter::setFont(const QFont &font)

      You reap what you sow it

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @peter-70 said in Create QIcon from a string:

        Unfortunately the icon will be drawn and I can see the text, but somehow impure.

        What size(s) do you use to display your icon?

        QIcon* CreateIcon(const QString& content, int iw, int ih)
        {
            auto pixmap = new QPixmap(iw, ih);
            auto painter = new QPainter(pixmap);
            painter->setPen(QColor(255, 0, 0, 128));
            painter->drawText(QRect(0, 0, iw, ih), content);
            return new QIcon(*pixmap);
        }
        

        Don't use new to create QIcon, QPixmap, and QPainter. (This won't make your icon look better, but this helps keep your code clean)

        QPixmap pixmap(iw, ih);
        QPainter painter(&pixmap);
        painter.setPen(QColor(255, 0, 0, 128));
        painter.drawText(QRect(0, 0, iw, ih), content);
        return QIcon(pixmap);
        

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        2
        • M Offline
          M Offline
          mvuori
          wrote on last edited by
          #4

          See if renderHint QPainter::TextAntialiasing for your painter makes a difference

          1 Reply Last reply
          1

          • Login

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