4

What is the best way to implement a toggle button with the Qt framework ?

EDIT:

The only possibility I am aware of is the QPushButton. According to Qt documentation the QPushButton::setCheckable(bool) allows to have a on/off behavior. How to make it appear such as below ? Knowing I use Qt 5.1.1

enter image description here

Laurent Mesguen
  • 354
  • 2
  • 6
  • 22
  • 3
    Maybe `QPushButton::setCheckable(bool)` property makes it toggle? – vahancho May 04 '18 at 13:21
  • http://doc.qt.io/qt-5/qpushbutton.html - search for "toggle" in the page – Mat May 04 '18 at 13:22
  • If I use such a function, how can I make my ToggleButton appear such as a toggle button ? knowing that I use Qt 5.1.1 – Laurent Mesguen May 04 '18 at 13:23
  • 1
    The simplest way is to use an image. Put one for pressed and one of released. – user3606329 May 04 '18 at 13:48
  • 1
    You can set background/border images to it using stylesheets. See [pseudo states](http://doc.qt.io/qt-5/stylesheet-syntax.html#pseudo-states) for stylesheets. – thuga May 04 '18 at 13:48
  • If you want to make the button feel like on iOS. See this https://stackoverflow.com/questions/14780517/toggle-switch-in-qt - it involves many lines of codes and anmiation. – user3606329 May 04 '18 at 13:50

1 Answers1

7

I'm not sure for QPushButton, but for QCheckboxes, you can set an icon as a stylesheet, meaning that you can give any appearance you want to your checkbox. So your "on" image could be the icon for state "checked" and your "off" image for state "unchecked" (http://doc.qt.io/archives/qt-4.8/stylesheet-examples.html#customizing-qcheckbox)

Andéol Evain
  • 507
  • 2
  • 12