PDA

View Full Version : QPainter with QSystemTrayIcon issues



adutzu89
14th January 2017, 17:23
Hello,

I'm trying to make system tray icon which contains text but it fails miserably.
Firstly the icon is made of 2 parts, one which contains text using a truetype icon fonts and another part with normal fonts.

One issues is that the application crashes, I cannot pinpoint the method which causes the crash but I think it's the draw method. I cannot find which is the issue because if I put breakpoints into the debugger the application works if don't it returns to normal behavior partly working.

Secondly:
If I put one part of the text into the pixmap with a size of 22 x 22 the text appears correctly, if I put it inside a 44 x 22 (w x h) pixmap the drawing is correctly placed but it becomes blurry, you should be able to find picture into attachments.


This is the code which shows the non-blurred number 4:


QPixmap tempPixmap(22, 22);
tempPixmap.fill(QColor(0,0,0,0));
QPainter tempPainter(&tempPixmap);
tempPainter.setPen(QColor(Qt::white));
tempPainter.drawText(tempPixmap.rect(), Qt::AlignCenter, "4");

QPixmap pixmap(22,22);
pixmap.fill(QColor(0,0,0,0));
QPainter iconPainter(&pixmap);
iconPainter.drawPixmap(QRect(0, 0, 22, 22), tempPixmap);
trayIcon->setIcon(QIcon(pixmap));



This is the code which shows the blurred version:


QPixmap tempPixmap(22, 22);
tempPixmap.fill(QColor(0,0,0,0));
QPainter tempPainter(&tempPixmap);
tempPainter.setPen(QColor(Qt::white));
tempPainter.drawText(tempPixmap.rect(), Qt::AlignCenter, "4");

QPixmap pixmap(44,22);
pixmap.fill(QColor(0,0,0,0));
QPainter iconPainter(&pixmap);
iconPainter.drawPixmap(QRect(22, 0, 22, 22), tempPixmap);
trayIcon->setIcon(QIcon(pixmap));


Can someone help me solve this issue?

anda_skoa
15th January 2017, 10:24
Any specific reason why you are not directly painting the text into the target pixmap?

Cheers,
_

adutzu89
15th January 2017, 10:37
Any specific reason why you are not directly painting the text into the target pixmap?

Cheers,
_

Hi anda, yes, the final result should be an icon based of text + icon, but the icon itself is from a font(like font awesome icons).
I have already tried drawing the text with the same pixmap and painter, but the end result it is the same, blurry text.

anda_skoa
15th January 2017, 14:16
I am not sure I understand why you need a second pixmap, but have you tried passing a point instead of a target rect?



iconPainter.drawPixmap(QPoint(22, 0), tempPixmap);


Cheers,
_

adutzu89
17th January 2017, 07:17
Same, text is blurry.

anda_skoa
17th January 2017, 10:21
Did you check if the image is blurry or just when it is being displayed?

Cheers,
_

adutzu89
25th January 2017, 09:44
Did you check if the image is blurry or just when it is being displayed?

Cheers,
_

I don't think I understand properly, sorry for my late response, how can I know if the image is blurry if I am not displaying it?

adutzu89
26th January 2017, 17:55
I don't think I understand properly, sorry for my late response, how can I know if the image is blurry if I am not displaying it?

I have tried saving the image and it's ok, the issue is when showing it in tray.

adutzu89
27th January 2017, 08:36
Apparently I cannot create rectangular icons because Windows does not allow it.

anda_skoa
27th January 2017, 09:48
That was my theory, hence asking to check if the image itself was OK (and yes, by saving it to file for example).

Cheers,
_