0

I would like to make a custom button that acts like a QPushButton (I specifically need the clicked signal and the whole checkable machinery setChecked(True)...).

I want my button to have an icon on the right hand side, vertically centered. And on the left hand side I would like to have 2 labels.

My first naïve attempt was to subclass QAbstractButton, and insert my widget in it like I would with a regular QWidget (giving it a layout, adding my subwidgets in it...). That did not work at all.

Do I need to start from a basic QWidget and implement myself the entire button machinery, or is there a better way that I am missing?

This is roughly the result I would like to achieve (first one is enabled and not checked, second one is checked, third one is disabled) :

enter image description here

PiRK
  • 893
  • 3
  • 9
  • 24
  • 7
    Why not subclass QPushButton? Also, have you looked at what you can do with Qt Style Sheets? – jwernerny Feb 13 '19 at 20:45
  • This might help to start on something : https://stackoverflow.com/q/44091339/6165833 (and this https://stackoverflow.com/a/16184337/6165833) – ymoreau Feb 14 '19 at 08:45
  • I'm ok with the stylesheets. What I do not understand is how to subclass a `QPushButton` while completly changing the content of the inside layout. I am under the impression that QPushButtons are limited to one piece of text and one icon. – PiRK Feb 14 '19 at 13:58
  • @ymoreau I have seen other exemples of subclassing QAbstractPushButton and override paintEvent, but again it seems limited to tweaking the way the one pixmap is drawn and the one text is printed. I haven't been able to find an example that creates a button with multiples labels (with each a different stylesheet), multiples images... – PiRK Feb 14 '19 at 14:02
  • 1
    A QPushButton itself is a widget, so you can add some child labels and use a QGridLayout to arrange them instead of using button's own text and icon. – Alexander Zavertan Feb 14 '19 at 14:26
  • I will try again, then. Yesterday I tried that using a QAbstractButton, but nothing was displayed – PiRK Feb 14 '19 at 16:15
  • 1
    Ok I confirm that it works with a QPushButton, but that the exact same code does not work with a QAbstractButton (which is a QWidget too). Thanks for the solution. There is something that I don't understand about QAbstractButton. – PiRK Feb 14 '19 at 16:27
  • May be, the "Abstract" in `QAbstractButton`? ;-) – Scheff's Cat Feb 15 '19 at 14:15

0 Answers0