[SOLVED] Button with Icons via code
-
wrote on 31 Jul 2015, 08:11 last edited by Andy314
I try to create a bool-indcator button. So I have some question about images.
I have a recoure file with windows ico files. Every iconfile has 3 incons integrated with different size. This is very practicable, because I can change in dialog designer the icon-size and it seem as it puts the best matching icon of the 3 for this size.But how can I do this via code?
QPixmap pix(":/images/Games.ico"); QIcon icon(pix); c->setIcon(icon); c->setIconSize(QSize(16,16));
This picks always the 48x48 icon and scales it to 16x16, what looks bad.
How can it pick a subicon with a special size from a resource file ???I want 2 different icon for the On and Off state for this button. Via Designer I can configure different icons for the 2 states (Normal on/Normal Off property).
How can I make this via code? I found only the function setIcon() which set on/off state to the same icon. -
wrote on 31 Jul 2015, 08:42 last edited by
Hi,
look at this: https://forum.qt.io/topic/14047/icon-sizes-incorrect/3 -
wrote on 31 Jul 2015, 08:44 last edited by
Hi,
instead of assigning the pixmap on QIcon constructor you shoud use addPixmap
QIcon icon; icon.addPixmap(onPixmap, QIcon::Normal, QIcon::On); icon.addPixmap(offPixmap, QIcon::Normal, QIcon::Off);
if you also need to set different pixmap depending on the size you can use
QIcon::addFile()
-
Hi,
instead of assigning the pixmap on QIcon constructor you shoud use addPixmap
QIcon icon; icon.addPixmap(onPixmap, QIcon::Normal, QIcon::On); icon.addPixmap(offPixmap, QIcon::Normal, QIcon::Off);
if you also need to set different pixmap depending on the size you can use
QIcon::addFile()
wrote on 31 Jul 2015, 08:47 last edited by@mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.
-
@mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.
wrote on 31 Jul 2015, 11:49 last edited by@Wieland said:
@mcosta Yes, seems like the QIcon from pixmap constructor needs some improvement.
The standard constructor is enough for standard usage.
Qt usually makes default things simple and provides API to use advanced feature (QMessageBox is another example) -
wrote on 31 Jul 2015, 14:09 last edited by
I found out the "read all subicons" is integreated in the constructor yet.
The problem was the indirect loading in a pixmap before.
Now, it takes really the best matching iconsize, automatically.QPushButton *c = new QPushButton(OnText, parent); QIcon icon(":/images/Games.ico"); c->setIcon(icon); c->setIconSize(QSize(16,16));
-
wrote on 31 Jul 2015, 14:13 last edited by
Cool! :-) Could you please post one of your icon files? I would like to check if this works with QtQuick, too.
-
Cool! :-) Could you please post one of your icon files? I would like to check if this works with QtQuick, too.
wrote on 31 Jul 2015, 15:56 last edited by@Wieland
how can I upload files here ?
1/8