Hello to all. There is a task, you need to make Qt widget as in the picture:

Imgur

QML does not roll. What ideas do you have? There is an idea to inherit from QSlider , redefine paintEvent , set minValue = 0 maxValue = 1 ? You can also try to inherit from QCheckBox , but here is also a question, adding a "slider" is the one that will do on / off.

  • qml does not roll this conclusion on the possibility of use or the condition of the problem? - aknew
  • this condition is jaroslav

2 answers 2

Good day!

Maybe I'm wrong, but in my opinion the problem is solved in this way:

1) Inherit from QAbstractButton or QCheckBox . What exactly you need to think about, since QCheckBox three states ("selected", "not selected", "partially allocated", see enum Qt::CheckState ) and it will have to be processed somehow. I would nevertheless inherit from QAbstractButton .

2) Redefine paitEvent(QPaintEvent*) anyway. Make your own drawing and preferably using QPalette.

3) I would suggest doing an animation like this:

a) Create a private class inherited from QObject (for QPropertyAnimation). The idiom of the Pointer To Implemetation (macros Q_DECLARE_PRIVATE and Q_D ) is very good for this. You will need to create a separate header file with the _p suffix (for example mybutton_p.h ) for the MOC to work properly. This approach is often used in the Qt library itself. More detail here and here .

b) We declare and implement in a private class all the necessary properties (slider position, text, etc.) while taking into account that when changing some properties, the widget itself needs to be redrawn.

c) Create a slot in a private class, for example, void animate(bool) . The parameter of this slot will be an indicator in which direction to play the animation.

d) Implement a slot using QPropertyAnimation. Here you can use the enum Direction for the correct playback direction and use qreal to interpolate the current slider position (for example, 0.0 is the leftmost ("off" state), 1.0 is the rightmost ("on" state)).

e) Connect the void animate(bool) slot of our private class with the clicked(bool) signal of the main class. This can be done in the constructor of the main class.

In conclusion, you can offer to search for all sorts of resources - maybe someone has already tried to implement something like that. In any case, it may push on some thoughts or ideas.

Successes!

UPD: implemented for the sake of interest - everything took about an hour of work. Here's what happened:

In off state: SlideSwitchOff

In the on state: alt text

Link to the project at SourceForge: https://sourceforge.net/projects/qslideswitcher/

Link to the study: https://ru.stackoverflow.com/research/289306/widgets-%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7 % 86% D0% B8% D1% 8F-qt-off-on-button-% D1% 81% D0% BE-% D1% 81% D0% BB% D0% B0% D0% B9% D0% B4% D0 % B5% D1% 80% D0% BE% D0% BC-% D0% B1% D0% B5% D0% B7-qml

  • Share the code, please. If you do not mind :) - jaroslav
  • I have a suggestion: can create a study? It's just that there is a lot of code to answer, and I would like to somehow answer something more extensively ... But here it seems to me somehow cumbersome) And I will share the code without questions - there is nothing tricky)) - progzdeveloper
  • "It's just that there is quite a lot of code to answer, and I would like to somehow answer something more extensively ..." +1 Can you upload code somewhere, if not difficult? - jaroslav
  • Eh ... I have never tried to use SourceForge and so on. So I will try) I just don’t know how quickly I’ll figure it out) As a last resort, I'll upload it to the file sharing service. Mark the answer as long as the answer is correct) Yes, do you create the research? - progzdeveloper
  • What is the essence of the research? I'm just here recently. I understand this is a more or less widely deployed question (answer to the question). Let's create, I think the topic may be useful to more than one. - jaroslav

if you do not take into account the option where the animated slider is used, then the problem is solved with the usual QCheckBox and qss styles otherwise, you need to implement the animation mechanism with your hands.

  • Yes, you need to take into account the animation. How in this case, then use the checkbox? How to implement the animation mechanism, if through qss you can substitute images for different states? QPropertyAnimation you will not fasten? I understand to draw a new widget (or inherit from QCheckBox). - jaroslav