Custom Qt Widgets: Implementing a Toggle Switch

preview_player
Показать описание
This video shows how to implement a custom Qt widget, when none of the widgets provided by Qt match your needs.

As an example, you'll be presented the code for a toggle switch, as is very common on mobile platforms, why not have something like that on the desktop too?

About the host:
This video is presented by David Faure, who has 24 years of experience with Qt, starting back in 1998 in the KDE project. Since 2003 he has taught many Qt training courses, on topics such as Qt Widgets, Multithreading, Modern C++, and Debugging/Profiling on Linux. He has made many contributions to QtWidgets and has written widget styles for numerous KDAB customers.

About KDAB:

KDAB experts regularly take time out to deliver KDAB’s world class training, in-house or at open enrollment courses around the world. We are the market leaders for training in Qt, OpenGL, and C++.

Рекомендации по теме
Комментарии
Автор

Very good video!

But I have a question: Isn't calling base class' event handler better programming style than calling event->ignore()? In case of inheriting from QWidget it does not matter (ignore() is called anyway and the event is passed to the parent), but when overriding any other class we usually want the event to be handled by the base class...

PS: I have a suggestion for another more advanced video about custom widgets. Some time ago I struggled with a widget which changed its size as a result of users interaction. Specifically I wanted to override QPlainTextEdit (or QTextEdit) which would fill the parent horizontally but it would grow vertically as user is adding text. In other words, a dynamically sized text edit which would never need scrollbars but it would automatically resize in vertical direction. I must say this was not easy... and I was struggling with resizing, updating layouts very badly... And I am still not sure whether I implemented it correctly.

vladimirkraus
Автор

Why are you blocking half of the "Projects" window, on the left side. Can't see the files!! Same with other video's you have

dalesmith
Автор

Maybe you need to add m_mouseDown = false in under the else. Imagine you made a mouse press over the widget and released mouse somewhere else. So, nothing happened, but your widget was 'armed' to react on the next release event. I hope my explanation was clear.

mykhailolytvyn
Автор

checked() and toggled() - why having toggled() is not good enough?

cernejr