PDA

View Full Version : How to add two icons/images to the same QPushButton??



ram4soft
8th March 2011, 15:50
Hi,
How to add two icons/images to the same QPushButton.
ie, i want to add one icon with alignment left and another in the center besdes to the text ??
i'm doing the following but it shows only one icon

championbtn=new QPushButton;
championbtn->setText("any");
championbtn->setIcon(QIcon(":/images/DEFAULT-LOGO.png"));
championbtn->setIcon(QIcon(":/images/CLOSE.png"));
championbtn->setIconSize(QSize(31,31));
thanks
Ram

BalaQT
8th March 2011, 15:56
setIcon will only set one image

championbtn->setIcon(QIcon(":/images/DEFAULT-LOGO.png"));
championbtn->setIcon(QIcon(":/images/CLOSE.png"));

in this code , the CLOSE.png will be set to the button.
simple solution will be just add the two images in imageeditor. and load it.

hope it helps
Bala

ram4soft
8th March 2011, 16:25
thanks bala,
i'm newbie to QT and don't know how to do that, so please give me sample code, link or more details
thanks

pastispast
1st April 2011, 13:09
Hi ram4soft,

Instead of you can merger two images and then apply them to the pushbutton

The syntax can be like this...


QIcon dummyIcon;
QPixmap comboPixmap(50, 25);
QPixmap firstImage(":/QTTestProject/images/Image-01.png"); //qrc path
QPixmap secondImage(":/QTTestProject/images/Image-02.png"); //qrc path

QPainter painter(&comboPixmap);
painter.drawPixmap(0, 0, firstImage);
painter.drawPixmap(firstImage.width(), 0, secondImage);

dummyIcon.addPixmap(comboPixmap);

championbtn->setIcon(dummyIcon);

Note: Set the comboPixmap size based on the "Image-01.png" and "Image-02.png".:)