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)
Author by
bam500
C/C++, Python dev in applicative software. Familiar with Git, Docker, Qt, CI/CD.
Updated on June 04, 2022Comments
-
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 theQPushButton::setCheckable(bool)
allows to have a on/off behavior. How to make it appear such as below ? Knowing I use Qt 5.1.1-
vahancho almost 6 yearsMaybe
QPushButton::setCheckable(bool)
property makes it toggle? -
Mat almost 6 yearsdoc.qt.io/qt-5/qpushbutton.html - search for "toggle" in the page
-
bam500 almost 6 yearsIf 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 almost 6 yearsThe simplest way is to use an image. Put one for pressed and one of released.
-
thuga almost 6 yearsYou can set background/border images to it using stylesheets. See pseudo states for stylesheets.
-
user3606329 almost 6 yearsIf 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.
-