문제

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?

도움이 되었습니까?

해결책

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);
   }

다른 팁

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!");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top