Converting Enums to and from Strings

preview_player
Показать описание
In your code, you will likely find numerous places where you go from an enum to a QString and back. This could, for example, be code where you serialize your data.

The code might very well look like this:

if (value == ExampleEnum::Foo)
return "Foo";
else if (value == ExampleEnum::Bar)
return "Bar";
~~~

But there is a smarter way to do it, using Qt's introspection facilities. Watch here to learn that trick.

The example code showcased in this video is available here:

All Qt Widgets and more videos:

About the host:
The video is presented by Jesper Pedersen, who started with Qt back when Qt was widgets only and the version was 1.44, which gives him 21 years of experience with Qt. Since 2000 he has taught almost 100 Qt classes, ranging from Qt Widgets to QML. Heck, Jesper even taught a few classes on QTopia Core — Trolltech's initial attempt at creating a phone stack. Today Jesper holds a fancy business title, but that doesn't prevent him from coding both in his job and in his spare time.

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++.

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

Thanks, you are very excellent teacher

piergiorgiovalli
Автор

FYI, the noquote method of qDebug only does something when printing a QChar/QString/QByteArray.
It does nothing when printing a const char pointer.

maartenofbelgium
Автор

Just heard the May11th training was cancelled :( Ugh, see you guys in August I guess. Love your teaching style!

matt_
Автор

Please prepare another video to use enums to qml and to what extent we can use in qml

badassopenpolling
Автор

With c++17 now, you can return a optional value for fromString. I believe this is more elegant.

彩虹
Автор

Dark mode exists. Thanks for blinding me.

callsofscv
Автор

screen not clear at all, I can barely see.

dianahskhosana