QT QML无法正确显示

时间:2019-01-08 12:01:31

标签: c++ qt visual-studio-2017 qml

我对使用QT相当陌生,但是在显示我在.qml文件中创建的应用程序窗口时遇到问题。我以QT youtube频道的教程为起点。当我运行该应用程序时,窗口确实会出现在屏幕上,但是,窗口没有达到指定的尺寸,也没有反映在qml中编写的内容(基于教程的结果)。我正在使用带有QT插件的VS2017,并且具有使用.ui文件显示的按钮和窗口。我在这里的实现有什么问题?

qml:

import QtQuick 2.9
import QtQuick.Controls 2.3
import QtPositioning 5.12
import QtLocation 5.12
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2

ApplicationWindow
{
    id: root
    width : 700
    height : 700
    visible : true
    title: qsTr("Hello World")

Action
{
    id: actionNew
    text: qsTr("&New")
    shortcut: StandardKey.New
    icon.name: "document-new"
    onTriggered: area.placeholderText = "To do = New"
}

Action
{
    id: actionOpen
    text: qsTr("&Open")
    shortcut: StandardKey.Open
    icon.name: "document-Open"
    onTriggered: area.placeholderText = "To do - Open"
}

    Action
{
    id: actionSave
    text: qsTr("&Save")
    shortcut: StandardKey.Save
    icon.name: "document-Save"
    onTriggered: area.placeholderText = "To do - Save"
}

menuBar: MenuBar
{
    Menu 
    {
        title: "File"
        MenuItem {action: actionNew}
        MenuItem {action: actionOpen}
        MenuItem {action: actionSave}
    }
}

header: ToolBar
{
    RowLayout
    {
        ToolButton {action: actionNew}
        ToolButton {action: actionOpen}
        ToolButton {action: actionSave}
    }
}

TextArea
{
    id: area
    anchors.fill: parent
}
}

main.cpp:

#include <QtQuick/QQuickView>
#include <QtQuick/QQuickItem>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QQuickView views;
    views.setSource(QUrl(QStringLiteral("qrc:/QTKM/main.qml")));
    views.show();


    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

测试代码,我看到出现了另一个空白窗口。

要解决此问题,我将主要内容更改为:

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

使用QQmlApplicationEngine代替QQuickView