Olivier Cléro

QtUtils

04/02/2022

Presentation

QtUtils is a set of tools to make using Qt5 more convenient.

I was tired of implementing again and again the same utilities company after company, so I decided to make an open-source library with these commonly used tools, so everyone can enjoy them.

Library Content

  • singleShotConnect: Allows to create a single-shot QMetaObject::Connection.

    1
    2
    3
    
    oclero::singleShotConnect(this, &SomeClass::someSignalTriggered, []() {
      // Do stuff.
    });
    
  • QtScopedConnection: RAII QMetaObject::Connection that will disconnect when destroyed.

    1
    2
    3
    
    oclero::QtScopedConnection scopedConnection = QObject::connect(this, &SomeClass::someSignalTriggered, []() {
      // Do stuff.
    });
    
  • QtEnumUtils: Utilities to convert enums to QString or int and vice-versa.

    1
    2
    
    auto str = oclero::enumToString(SomeEnum::SomeValue);
    auto value = oclero::enumFromString<SomeEnum>("SomeValue");
    
  • QtEventFilterUtils: Utilities to quickly register to an event without the burden to create a class. All corresponding QEvent-derived classes have been mapped to their corresponding QEvent::Type, so you don’t even have to cast the QEvent or worry about its type.

    1
    2
    3
    4
    
    oclero::EventFilter<QEvent::MouseButtonPress>::install(watchedObject, [](QMouseEvent* e) {
      // Do stuff, then return 'true' to block the event propagation, 'false' otherwise.
      return false;
    });
    
  • QtDeleteLaterScopedPointer: A pointer manager like std::unique_ptr or QScopedPointer, but calls deleteLater() instead of delete.

    1
    
    oclero::QtDeleteLaterScopedPointer<QObject> scopedPointer(rawPointer);
    
  • QtSettingsUtils: Utilities to save and load strongly typed values from QSettings.

    1
    2
    3
    4
    
    QSettings settings;
    auto value = oclero::loadSetting<int>(settings, "key", 3 /* default value */);
    oclero::saveSetting(settings, "key", 3);
    oclero::clearSetting(settings, "key");
    

Information