How to create a QIcon with Qt programmatically

First, create a 32×32 QImage and make it transparent (or whatever background color you need):

QImage img(32, 32, QImage::Format_ARGB32);
img.fill(QColor(0, 0, 0, 0));

I use QImage instead of QPixmap, because the documentation says that QImage is optimized for I/O, e.g. painting, and the first attempts with QPixmap looked like crap. Next, create a QPainter for the QPaintDevice and set some render hints to make it look nice:

QPainter *p = new QPainter(&img);
p->setRenderHint(QPainter::Antialiasing);
p->setRenderHint(QPainter::TextAntialiasing);
p->setRenderHint(QPainter::SmoothPixmapTransform);

QPainter::setBrush() sets the background color for shapes, QPainter::setPen() the foreground color for text:

p->setBrush(QColor(Qt::red));
p->setPen(QColor(Qt::white));

Then select a font the size of our future Icon:

QFont f("courier new");
f.setPixelSize(32);
p->setFont(f);

Now we need some background. White on transparent isn’t really readable, so let’s draw a circle:

p->drawEllipse(img.rect());

Since our QImage is actually a square and not an rectangle, QPainter::drawEllipse will draw a circle. Print the QChar, letter, or whatever:

p->drawText(img.rect(), Qt::AlignCenter, QChar(char));

Now clean up and return the QImage as QIcon:

delete p;
return QIcon(QPixmap::fromImage(img));

The whole shebang can be marveled at here. It looks like this:

Have fun!

Update QT under Windows

Well, another lesson in How To Complicate Things ™. Could be as easy as pacman -Syu, run qmake and recompile all, but far from it 🙁

  1. Run MaintenanceTool.exe. Of course it has no entry in the start menu, why should it. But it’s easy to find: C:\Qt\MaintenanceTool.exe
  2. Skip the first page, you don’t need an account. God, how I hate this shit!
  3. On the next page, select “Add or remove components”. “Update components” will only update your *current* version. To install a *new* version, you have select the first option.
  4. Wait…
  5. Select the version you want to install and wait, but take a look while it’s installing. Ignore the errors, it’ll work anyway

Once the installer finished with errors, open QtCreator and load your project. Now you have to manage your “Kit”. Select “Project” on the left sidebar, “Build & Run” on top and then click “Manage kits…”

  • Click “Add” and select the new version for “Debugger” and “Qt version”
  • Give it a “Name:”. Best to copy the string from the Auto-detected version
  • Apply
  • Now change the Kit of your project to the new one. The thingy above “Build|Run” right beside “Manage Kits…” is a drop down box, even if it doesn’t look like it
  • Change the build directory accordingly
  • Clear the environment by selecting “Build” instead of “Run”. Then expand “Build Environment” at the bottom and check “Clear system environment”
  • Select “Run” and set “Base environment for this configuration” to “Build environment”

If you don’t need SQL-Plugins, clean all, run qmake and compile. If you need access to, say, a Postgres-DB, the fun doesn’t end yet. To connect to the database, you have to add the directory with libpq.dll to the PATH. Yes, to the PATH environment variable! WTF? For now that would be “C:\psql\9.5\bin”.

If you create a new project, don’t forget to add

QT       += sql
LIBS     += "C:\psql\9.5\lib\libpq.lib"

to you .pro-file.

If you build a release, don’t forget to copy the dll-files to the release directory. For a QT-Gui application those would be at least:

  • libeay32.dll
  • libecpg.dll
  • libecpg_compat.dll
  • libgcc_s_dw2-1.dll
  • libiconv-2.dll
  • libintl-8.dll
  • libpgtypes.dll
  • libpq.dll
  • libstdc++-6.dll
  • libwinpthread-1.dll
  • libxml2.dll
  • libxslt.dll
  • Qt5Core.dll
  • Qt5Gui.dll
  • Qt5Sql.dll
  • Qt5Widgets.dll
  • ssleay32.dll
  • zlib1.dll

Well, I hope I didn’t forget anything. See you next time!