An open API service indexing awesome lists of open source software.

https://github.com/huaiminnotsleepyet/nwidget2


https://github.com/huaiminnotsleepyet/nwidget2

declarative-ui gui qt qwidget

Last synced: about 1 year ago
JSON representation

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());
```

![](./docs/imgs/property_binding.gif)

## 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()));
```

![](./docs/imgs/animation.gif)

## 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),
},
}}};
```

![](./docs/imgs/declarative_ui_syntax.png)

## 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)

![](./docs/imgs/MCDayNightSwitchButton.gif)

## Special Thanks

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