Icons for pushbutton
-
I would like to have my own icons on some pushbuttons. I have two icons, one for the released state and one for the pressed state.
I tried it like this but this didn't work :@ QIcon icon;
QImage im1(":/Images/checked.png");
icon.addPixmap(QPixmap::fromImage(im1),QIcon::Normal, QIcon::On);
icon.addPixmap(QPixmap::fromImage(im1),QIcon::Active, QIcon::On);
icon.addPixmap(QPixmap::fromImage(im1),QIcon::Selected, QIcon::On);
QImage im2(":/Images/unchecked.png");
icon.addPixmap(QPixmap::fromImage(im2),QIcon::Normal, QIcon::Off);
icon.addPixmap(QPixmap::fromImage(im2),QIcon::Active, QIcon::Off);
icon.addPixmap(QPixmap::fromImage(im2),QIcon::Selected, QIcon::Off);QPushButton *pb = new QPushButton (this);
pb->setIcon(icon);
pb->setIconSize(pb->size());
pb->setFocusPolicy(Qt::NoFocus);@
I always have only one icon.
What do I miss ? If I add the icons to the different states of the icon, I should get the right icons automatically, no ? -
I'm sorry, your code is wrong. Basiclly, you can done this by using style-sheet. Or, listening the press/release events, and handle the icon setting yourself.
Something like this by using style-sheet:
@
QPushButton:pressed {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #dadbde, stop: 1 #f6f7fa);
}
@ -
You can do it in Qt Creator when editing your form.
Use a resource file with the needed images.select your pushbutton and go to the properties.
select an icon.
You will see severall states. make sure you pick the right one forActive On and Active off.That's it.
-
this is the code taken from my ui_dialog.h file
This should help you out with your code. Look at the red.png line : all the others remain the same.
@ QIcon icon; icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Normal, QIcon::On); icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Disabled, QIcon::Off); icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Active, QIcon::Off); icon.addFile(QString::fromUtf8(":/red.png"), QSize(), QIcon::Active, QIcon::On); icon.addFile(QString::fromUtf8(":/blue.png"), QSize(), QIcon::Selected, QIcon::Off); pushButton->setIcon(icon); pushButton->setCheckable(true);@
i really like to do things like this using Qt Designer and see all the options immediately in the dialog.