In QT: I only want to show an icon and some text, so I use QPushButton. But how can I remove the click effect from it?
Asked
Active
Viewed 2,166 times
0
-
What do you mean 'click' effect? Do you mean the 3D bevel? – cmannett85 Sep 28 '12 at 09:44
-
I think he wants to use QPushButton basically as a QLabel with Text and Icon displayed at the same time ... – Andreas Fester Sep 28 '12 at 09:53
2 Answers
2
You can subclass QPushButton and ignore all events but the Paint event:
class IconLabel : public QPushButton {
...
bool IconLabel::event ( QEvent * e ) {
if (e->type() == QEvent::Paint) {
return QPushButton::event(e);
}
return true;
}
Depending on your requirements, it might be necessary to let additional events pass through, like if you want to use a tooltip on your IconLabel:
if (e->type() == QEvent::Paint ||
e->type() == QEvent::ToolTip) {
return QPushButton::event(e);
}

Andreas Fester
- 36,091
- 7
- 95
- 123
2
I haven't tried this solution but looks like it should work.
Copying from the link above
Use rich text for the label, e.g:
lbl->setTextFormat(Qt::RichText);
lbl->setText("<img src=":/myimage.png">Hello!");

dev
- 2,180
- 2
- 30
- 46
-
Nice solution :-) Just tried it and it works well (slightly modified the setText() call: `lbl->setText("
Hello!");`, it also depends on whether you are using a resource or a local file). The style is a bit different from the QPushButton approach, e.g. no frame, but this depends on the OPs specific requirements and can surely be adjusted by some stylesheet. – Andreas Fester Sep 28 '12 at 11:15