0

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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Al2O3
  • 3,103
  • 4
  • 26
  • 52

2 Answers2

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