https://github.com/oclero/qtutils
A set of tools for Qt5.
https://github.com/oclero/qtutils
cmake cpp qt5
Last synced: 5 months ago
JSON representation
A set of tools for Qt5.
- Host: GitHub
- URL: https://github.com/oclero/qtutils
- Owner: oclero
- License: mit
- Created: 2022-02-03T18:06:16.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-02-18T13:38:32.000Z (about 2 years ago)
- Last Synced: 2024-02-18T14:34:27.702Z (about 2 years ago)
- Topics: cmake, cpp, qt5
- Language: C++
- Homepage:
- Size: 43.9 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# QtUtils
[](https://mit-license.org/)
[](https://www.cmake.org)
[](https://en.cppreference.com)
[](https://www.qt.io)
**QtUtils** is a set of basic utilities that I consider should be part of Qt API. It contains many helpers that improve the Qt experience, from dealing with `enum` to usage of pointers.
---
### Table of Contents
- [Requirements](#requirements)
- [Usage](#usage)
- [Content](#content)
- [Author](#author)
- [License](#license)
---
## Requirements
- Platform: Windows, MacOS, Linux.
- [CMake 3.21+](https://cmake.org/download/)
- [Qt 6.0.0+](https://www.qt.io/download-qt-installer)
## Usage
1. Add the library as a dependency with CMake FetchContent.
```cmake
include(FetchContent)
FetchContent_Declare(QtUtils
GIT_REPOSITORY "https://github.com/oclero/qtutils.git"
)
FetchContent_MakeAvailable(QtUtils)
```
2. Link with the library in CMake.
```cmake
target_link_libraries(your_project oclero::QtUtils)
```
3. Include headers in your C++ file.
```c++
#include
```
## Content
### QtConnectionUtils
#### Single-shot Connection
This connection handling will be called only once.
```cpp
oclero::singleShotConnect(this, &SomeClass::someSignalTriggered, []() {
// Do stuff.
});
```
### QtScopedConnection
- This connection will be closed when it is destroyed, i.e. when the scope it belongs ends. It's a RAII `QMetaObject::Connection`.
```cpp
oclero::QtScopedConnection scopedConnection = QObject::connect(this, &SomeClass::someSignalTriggered, []() {
// Do stuff.
});
```
- This `QMetaObject::Connection` container will close its content RAII-style.
```cpp
oclero::QtScopedConnections scopedConnections;
scopedConnections.add(QObject::connect(this, &SomeClass::someSignalTriggered, []() {
// Do stuff.
}));
```
### QtEnumUtils
- Converts `enum` to `QString` and vice-versa. Checks if the value is valid.
```cpp
auto str = oclero::enumToString(SomeEnum::SomeValue);
auto value = oclero::enumFromString("SomeValue");
```
- Converts `enum` to `int` and checks if the value is valid.
```cpp
auto value = oclero::enumFromInt(3);
```
### QtEventFilterUtils
Tired of writing a new class just to hook on an event? Now you can just write this:
```cpp
oclero::EventFilter::install(watchedObject, [](QMouseEvent* e) {
// Do stuff.
// Return 'true' to block the event propagation, 'false' otherwise.
return false;
});
```
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.
### QtPointerUtils
- A unique pointer that will call `QObject::deleteLater` instead of `delete` when its scope ends.
```cpp
oclero::QtDeleteLaterScopedPointer scopedPointer(rawPointer);
```
### QtSettingsUtils
Utilities to make saving and retrieving values with `QSettings` more convenient.
```cpp
QSettings settings;
auto value = oclero::loadSetting(settings, "key", 3 /* default value */);
oclero::saveSetting(settings, "key", 3);
oclero::clearSetting(settings, "key");
```
## Author
**Olivier Cléro** | [email](mailto:oclero@pm.me) | [website](https://www.olivierclero.com) | [github](https://www.github.com/oclero) | [gitlab](https://www.gitlab.com/oclero)
Thanks to Thomas Donel for his help.
## License
**QtUtils** is available under the MIT license. See the [LICENSE](LICENSE) file for more info.