You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, April 18th 2007, 4:44pm

QT4: Button with two icons

Hi,

I want to create a button which displays an icon1 when it is not pressed and an icon2 when it is pressed. I tried it with the following code, but the button has a background wenn it ist pressed. What can I do to avoid this. Or can I do this in a better way?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	QPixmap buttonImage(":/images/button.png");
	QPixmap button2Image(":/images/button2.png");
	
	QIcon Icon;
	
	Icon.addPixmap ( buttonImage, QIcon::Normal, QIcon::Off );
	Icon.addPixmap ( button2Image, QIcon::Normal, QIcon::On );
	
	QPushButton *Button = new QPushButton();
	
	Button->setFlat(true);
	Button->setIcon(Icon);
	Button->setIconSize(QSize(100,100));
	
	Button->setCheckable(true);

This post has been edited 1 times, last edit by "justXi" (Apr 18th 2007, 4:45pm)


saubue

Trainee

Posts: 129

Location: Germany

  • Send private message

2

Wednesday, April 18th 2007, 9:27pm

After a quick look into the documentation, I think you either have to make your own widget that acts like a QIcon or to subclass QIconEngine and overwrite 'paint()'. I would prefer the first method; the documentation about QIcon provides some details about it.