Qt QML简单程序未运行

Qt QML简单程序未运行,qt,qml,qt5,qtquick2,Qt,Qml,Qt5,Qtquick2,我正在尝试运行以下命令,但运行时什么也没发生。 如何调试这样的问题 import QtQuick 2.0 import QtQml.Models 2.1 Item{ id: main width: 1500 height: 1500 GridView { id: root width: 1500 height: 1500 cellWidth: 200; cellHeight: 200

我正在尝试运行以下命令,但运行时什么也没发生。 如何调试这样的问题

import QtQuick 2.0
import QtQml.Models 2.1



Item{
    id: main
    width: 1500
    height: 1500
    GridView {
        id: root
        width: 1500
        height: 1500
        cellWidth: 200; cellHeight: 200
        visible: true


        model: DelegateModel {
            model: ListModel {
                ListElement {
                    color: "blue"
                }
                ListElement {
                    color: "white"
                }
                ListElement {
                    color: "red"
                }
                ListElement {
                    color: "green"
                }
                ListElement {
                    color: "orange"
                }
                ListElement {
                    color: "yellow"
                }
                ListElement {
                    color: "grey"
                }
            }

            delegate: MouseArea {
                objectName: "mousearea"

                implicitHeight: parent.height
                implicitWidth: parent.width

                Rectangle {
                    anchors.fill: parent
                    color: model.color
                }
                drag{
                    target: parent
                }
            }
        }
    }
}
我从本准则中的意图如下: 在
GridView
中创建几个矩形,并向其添加
MouseArea
,然后尝试拖动它们。我不确定我的模型结构是否正确

编辑: 添加main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

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

    return app.exec();
}
#包括
#包括
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
if(engine.rootObjects().isEmpty())
返回-1;
返回app.exec();
}

QQmlApplicationEngine希望有一个窗口作为根元素,如下所示:


与QQuickView不同,QQmlApplicationEngine不会自动创建 根窗口。如果您使用的是来自Qt Quick的可视项目,则 需要把它们放在窗户里面。

因此,解决方案很简单,按窗口逐项更改:

main.qml

import QtQuick 2.0
import QtQuick.Window 2.11
import QtQml.Models 2.1

Window{
    visible: true
    id: main
    width: 1500
    height: 1500
    GridView {
        id: root
        width: 1500
        height: 1500
        cellWidth: 200; cellHeight: 200
        visible: true


        model: DelegateModel {
            model: ListModel {
                ListElement {
                    color: "blue"
                }
                ListElement {
                    color: "white"
                }
                ListElement {
                    color: "red"
                }
                ListElement {
                    color: "green"
                }
                ListElement {
                    color: "orange"
                }
                ListElement {
                    color: "yellow"
                }
                ListElement {
                    color: "grey"
                }
            }

            delegate: MouseArea {
                objectName: "mousearea"

                implicitHeight: parent.height
                implicitWidth: parent.width

                Rectangle {
                    anchors.fill: parent
                    color: model.color
                }
                drag{
                    target: parent
                }
            }
        }
    }
}

什么意思,但当我运行它时什么都没有发生?窗口没有显示任何内容或应用程序关闭?什么都没有显示,但应用程序仍在运行请显示你的main.cpp