Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt with WinRT C++ build issue



  • Hello!

    I want to build a modern Windows application using WinRT (Windows 10). I use Qt 5.13.1 UWP kits for Visual Studio 2017. When building a project, it displays a lot of compilation errors:

    2019-10-16_143454.png

    Code:

    testproject.pro

    QT += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++17
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        testproject.cpp
    
    HEADERS += \
        testproject.h
    
    FORMS += \
        testproject.ui
    
    LIBS += -lwindowsapp
    
    # Default rules for deployment.
    #qnx: target.path = /tmp/$${TARGET}/bin
    #else: unix:!android: target.path = /opt/$${TARGET}/bin
    #!isEmpty(target.path): INSTALLS += target
    

    testproject.h

    #ifndef TESTPROJECT_H
    #define TESTPROJECT_H
    
    #include <QDialog>
    #include <QDebug>
    #include "winrt/Windows.System.Diagnostics.h"
    using namespace winrt;
    using namespace Windows::System::Diagnostics;
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class TestProject; }
    QT_END_NAMESPACE
    
    class TestProject : public QDialog
    {
        Q_OBJECT
    
    public:
        TestProject(QWidget *parent = nullptr);
        ~TestProject();
    
    private:
        Ui::TestProject *ui;
    };
    #endif // TESTPROJECT_H
    

    testproject.cpp

    #include "testproject.h"
    #include "ui_testproject.h"
    
    TestProject::TestProject(QWidget *parent)
        : QDialog(parent)
        , ui(new Ui::TestProject)
    {
        ui->setupUi(this);
        init_apartment();
    
        auto info = SystemDiagnosticInfo::GetForCurrentSystem();
        auto memory = info.MemoryUsage().GetReport().TotalPhysicalSizeInBytes();
        qDebug() << memory;
    }
    
    TestProject::~TestProject()
    {
        delete ui;
    }
    

    Without WinRT code it compiles successfully. Any ideas how to fix these issues? Thanks.



  • Also, I have tried to set a color for the titlebar:

    auto titleBar = ApplicationView::GetForCurrentView()->TitleBar;
    titleBar->BackgroundColor = ColorHelper::FromArgb(211, 211, 211, 211);
    titleBar->ButtonBackgroundColor = ColorHelper::FromArgb(211, 211, 211, 211);
    

    But these issues still exists:

    2019-10-16_193027.png

    I think something is missing using Qt, because the same project complies successfully using Visual Studio 2017 / 2019.



  • I tried to run @Chris-Kawa example from here: https://forum.qt.io/topic/68670/how-can-i-use-winrt-c-apis-in-at-qt-winrt-app/13

    It returns the following issues:
    2019-10-17_175719.png

    Any ideas how to configure WinRT using Qt? What libraries are required for Qt to run WinRT code? Thanks in advance.



  • I have fixed the issue by adding #undef X64 before #include "winrt/Windows.System.Diagnostics.h". For some reason this define conflicts with the ProcessorArchitecture::X64 enum in Windows.System.0.h as stated by VVV user from: https://stackoverflow.com/questions/58436766/qt-with-winrt-c-build-issue/58440774#58440774. The issue is resolved.


Log in to reply