Best way to implement a toggle button with Qt

10,984

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)

Share:
10,984
bam500
Author by

bam500

C/C++, Python dev in applicative software. Familiar with Git, Docker, Qt, CI/CD.

Updated on June 04, 2022

Comments

  • bam500
    bam500 almost 2 years

    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

    • vahancho
      vahancho almost 6 years
      Maybe QPushButton::setCheckable(bool) property makes it toggle?
    • Mat
      Mat almost 6 years
      doc.qt.io/qt-5/qpushbutton.html - search for "toggle" in the page
    • bam500
      bam500 almost 6 years
      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
    • user3606329
      user3606329 almost 6 years
      The simplest way is to use an image. Put one for pressed and one of released.
    • thuga
      thuga almost 6 years
      You can set background/border images to it using stylesheets. See pseudo states for stylesheets.
    • user3606329
      user3606329 almost 6 years
      If you want to make the button feel like on iOS. See this stackoverflow.com/questions/14780517/toggle-switch-in-qt - it involves many lines of codes and anmiation.