https://github.com/huaiminnotsleepyet/nwidget2
https://github.com/huaiminnotsleepyet/nwidget2
declarative-ui gui qt qwidget
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/huaiminnotsleepyet/nwidget2
- Owner: HuaiminNotSleepYet
- License: mit
- Created: 2025-03-29T17:30:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-29T13:15:26.000Z (about 1 year ago)
- Last Synced: 2025-05-05T22:53:45.015Z (about 1 year ago)
- Topics: declarative-ui, gui, qt, qwidget
- Language: C++
- Homepage:
- Size: 554 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# nwidget2
nwidget is a `header-only` library that provides `property binding`, `animation`, `declarative syntax` extension for QWidget.
## Template Meta-Object System
```cpp
#include
using namespace nwidget;
using MetaObj = MetaObject;
using MetaProp = decltype(MetaObj().fullScreen());
using Class = MetaObj::Class; // QWidget
using SuperClass = MetaObj::Super::Class; // QObject
using Type = MetaProp::Type; // bool
constexpr bool readable = MetaProp::isReadable; // true
constexpr bool writable = MetaProp::isWritable; // false
```
## Property Binding
```cpp
#include
using namespace nwidget;
auto label = MetaObject<>::from(new QLabel);
auto slider1 = MetaObject<>::from(new QSlider);
auto slider2 = MetaObject<>::from(new QSlider);
label.text() = nwidget::asprintf_("%d", slider1.value() + slider2.value());
```

## Animation
```cpp
#include
using namespace nwidget;
auto rect = MetaObject<>::from(new QWidget);
auto checkBox = MetaObject<>::from(new QCheckBox);
rect.styleSheet() = "background: #FF0000";
Behavior::on(rect.minimumWidth(),
new SpringAnimation(
spring{2},
damping{0.2}));
cond(checkBox.checked(), 300, 50).bindTo(Behavior::animated(rect.minimumWidth()));
```

## Declarative UI Syntax
```cpp
#include
using namespace nwidget;
QLayout* layout = FormLayout{
{"Label 0", LineEdit().text("Hello")},
{"Label 1", Slider(Qt::Horizontal).range(0, 100).value(25)},
{GridLayout{
{0, 0, PushButton("Button 0")},
{1, 0, PushButton("Button 1")},
{0, 1, 2, 1, PushButton("Button 2").sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding),
},
}}};
```

## Header Files
| Header File | Description |
| ------------- | ---------------------------------------------------------------- |
| behavior.h | Animation and Behavior |
| binding.h | Property Binding |
| builder.h | Declarative UI Syntax Builder |
| builders.h | Builder specialization for Qt classes, include after Qt headers |
| metaobject.h | Template Meta-Object System |
| metaobjects.h | Template specialization for Qt classes, include after Qt headers |
## Examples
[MCDayNightSwitchButton](./examples/MCDayNightSwitchButton)

## Special Thanks
- [@QuadnucYard](https://github.com/QuadnucYard) for guidance on C++.
- [@Niwik](https://github.com/niwik-dev) for suggestions on the property binding.