Lv.的博客

RxCpp(四)Qt/GUI编程

Qt简介

Qt(发音可爱)生态系统是一个全面的基于c++的框架,用于编写跨平台和多平台GUI应用程序。如果使用库的可移植核心编写程序,则可以利用框架支持的一次编写和到处编译范例。在某些情况下,人们使用特定于平台的特性,比如支持Activex编程模型来编写基于windows的应用程序。
在windows中编写应用程序时,我们会遇到qt优于mfc的情况。这样做的一个合理原因可能是易于编程,因为qt为其库使用了非常小的c++语言特性子集。当然,框架的最初目标是跨平台开发。Qt的跨平台的单一源代码可移植性、特性的丰富性、源代码的可用性以及更新良好的文档,使其成为一个非常适合程序员的框架。自1995年第一次发行以来,这帮助它在20多年的时间里蓬勃发展。
Qt提供了一个完整的接口环境,支持开发多平台GUI应用程序、webkit API、媒体、流媒体、文件系统浏览器、OpenGL API等等。


rxqt简介

rxqt库是一个写在rxcpp库上的公共域库,它使用qt事件和信号以反应性的方式进行编程变得很容易。为了理解这个库,让我们进入一个示例,这样我们就可以跟踪鼠标事件,并使用库提供的observable来过滤它们。该库的下载以及更多示例见:rxqt:


复制代码
#include <QApplication>
#include <QLabel>
#include <QWidget>
#include <QVBoxLayout>
#include <QMouseEvent>
#include "rxqt.hpp"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    // Create the application window
    auto widget = std::unique_ptr<QWidget>(new QWidget());
    widget->resize(280, 200);

    // Create and set properties of mouse area label
    auto label_mouseArea = new QLabel("Mouse Area");
    label_mouseArea->setMouseTracking(true);
    label_mouseArea->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    label_mouseArea->setFrameStyle(2);

    // Create and set properties of message display label
    auto label_coordinates = new QLabel("X = 0, Y = 0");
    label_coordinates->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    label_coordinates->setFrameStyle(2);

    // Adjusting the size policy of widgets to allow stretching inside the vertical layout
    label_mouseArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label_coordinates->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    auto layout = new QVBoxLayout;
    layout->addWidget(label_mouseArea);
    layout->addWidget(label_coordinates);
    layout->setStretch(0, 4);
    layout->setStretch(1, 1);
    widget->setLayout(layout);

    // Display the mouse move message and the mouse coordinates
    rxqt::from_event(label_mouseArea, QEvent::MouseMove)
        .subscribe([&label_coordinates](const QEvent* e) {
        auto me = static_cast<const QMouseEvent*>(e);
        label_coordinates->setText(QString("Mouse Moving : X = %1, Y = %2")
            .arg(me->x())
            .arg(me->y()));
            });

    // Display the mouse signle click message and the mouse coordinates
    rxqt::from_event(label_mouseArea, QEvent::MouseButtonPress)
        .subscribe([&label_coordinates](const QEvent* e) {
        auto me = static_cast<const QMouseEvent*>(e);
        label_coordinates->setText(QString("Mouse Single click at X = %1, Y = %2")
            .arg(me->x())
            .arg(me->y()));
            });

    // Display the mouse double click message and the mouse coordinates
    rxqt::from_event(label_mouseArea, QEvent::MouseButtonDblClick)
        .subscribe([&label_coordinates](const QEvent* e) {
        auto me = static_cast<const QMouseEvent*>(e);
        label_coordinates->setText(QString("Mouse Double click at X = %1, Y = %2")
            .arg(me->x())
            .arg(me->y()));
            });

    widget->show();
    return app.exec();
} // End of main
复制代码

 

posted @   Avatarx  阅读(341)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2016-05-29 使用资源调控器管理资源
2016-05-29 SQL Server 2008新特性——策略管理
2016-05-29 导出SQL Server中所有Job的最简单方法
2016-05-29 SQL Server作业的备份
2016-05-29 Sqlserver 数据库
2016-05-29 SQLSERVER 数据库管理员的专用连接DAC
2014-05-29 基于sqlite的Qt 数据库封装
点击右上角即可分享
微信分享提示