| 9 | #include <QVBoxLayout> |
| 10 | |
| 11 | int main(int argc, char* argv[]) |
| 12 | { |
| 13 | QApplication app{argc, argv}; |
| 14 | |
| 15 | auto button_1 = new QPushButton("Click me!"); |
| 16 | auto button_2 = new QPushButton("Click me!"); |
| 17 | auto clicks_count_label = new QLabel(); |
| 18 | auto clicks_duration_label = new QLabel(); |
| 19 | |
| 20 | const auto clicks_1 = rppqt::source::from_signal(*button_1, &QPushButton::clicked); |
| 21 | const auto clicks_2 = rppqt::source::from_signal(*button_2, &QPushButton::clicked); |
| 22 | const auto merged_clicks = clicks_1 | rpp::operators::merge_with(clicks_2); |
| 23 | |
| 24 | const auto total_clicks = merged_clicks | rpp::operators::scan(0, [](int seed, auto) { return ++seed; }); |
| 25 | const auto click_times = merged_clicks | rpp::operators::map([](auto) { return std::chrono::high_resolution_clock::now(); }); |
| 26 | const auto time_since_click = rpp::source::interval(std::chrono::milliseconds{1}, rppqt::schedulers::main_thread_scheduler{}) |
| 27 | | rpp::operators::with_latest_from([](auto, const auto click_time) { return std::chrono::high_resolution_clock::now() - click_time; }, click_times); |
| 28 | |
| 29 | // ..... |
| 30 | |
| 31 | total_clicks.subscribe([&clicks_count_label](int clicks) { |
| 32 | clicks_count_label->setText(QString{"Clicked %1 times in total!"}.arg(clicks)); |
| 33 | }); |
| 34 | |
| 35 | |
| 36 | time_since_click.subscribe([&clicks_duration_label](std::chrono::high_resolution_clock::duration ms) { |
| 37 | clicks_duration_label->setText(QString{"MS since last click %1!"}.arg(std::chrono::duration_cast<std::chrono::milliseconds>(ms).count())); |
| 38 | }); |
| 39 | |
| 40 | QMainWindow window{}; |
| 41 | auto vbox = new QVBoxLayout(); |
| 42 | vbox->addWidget(button_1); |
| 43 | vbox->addWidget(button_2); |
| 44 | vbox->addWidget(clicks_count_label); |
| 45 | vbox->addWidget(clicks_duration_label); |
| 46 | window.setCentralWidget(new QWidget); |
| 47 | window.centralWidget()->setLayout(vbox); |
| 48 | window.show(); |
| 49 | |
| 50 | return app.exec(); |
| 51 | } |
nothing calls this directly
no test coverage detected