QT Demo 之 window

这篇博客介绍了在QT GUI编程中,如何使用QQuickWindow显示QML场景。从QGuiApplication的初始化和主消息循环,到QQmlEngine和QQmlComponent的使用,再到QQuickWindow的详细讲解,以及qWarning()函数在调试中的应用。文章通过源码分析,逐步解析了每个关键组件的作用和用法。
摘要由CSDN通过智能技术生成

我们开始学习GUI编程中的一个超级重要的概念,那就是Window,不过在Qt中有很多种Windows,今天我学习的是QQuickWindow,顾名思义,就是用来显示QML场景的窗口。

在这一章,我们从cpp代码开始分析,先看一下main.cpp的代码:

int main(int argc, char* argv[])
{
    QGuiApplication app(argc, argv);
    QQmlEngine engine;
    QQmlComponent component(&engine);
    QQuickWindow::setDefaultAlphaBuffer(true);
    component.loadUrl(QUrl("qrc:///window/window.qml"));
    if ( component.isReady() )
        component.create();
    else
        qWarning() << component.errorString();
    return app.exec();
}
上面的代码包含以下几个知识点:

  • QGuiApplication
  • QQmlEngine
  • QQmlComponent
  • QQuickWindow
  • qWarning

下面就针对每一个知识点进行详细讲解。

QGuiApplication

先从官方文档上了解一下QGuiApplication的简述:

The QGuiApplication class manages the GUI application's control flow and main settings.
这句话整体说明了QGuiApplication类是用来进行GUI的控制流程和主设定相关。

QGuiApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization and finalization, and provides session management. In addition, QGuiApplication handles most of the system-wide and application-wide settings.
这一段说明了QGuiApplication包含了主消息循环处理,用来进行系统消息和其它消息的处理和分发。并且也完成了程序的初始化和销毁,以及提供整个的会话管理。

For any GUI application using Qt, there is precisely one QGuiApplication object no matter whether the application has 0, 1, 2 or more windows at any given time. For non-GUI Qt applications, use QCoreApplication instead, as it does not depend on the Qt GUI module. For QWidget based Qt applications, use QApplication instead, as it provides some functionality needed for creating QWidget instances.
这一段强调了一个程序只能有一个QGuiApplication对象,而且如果不是GUI程序也请不要使用QGuiApplication类。

这一章的例子中仅仅使用到了QGuiApplication的创

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值