Qt QML自定义项不与QQML应用程序引擎一起工作

Qt QML自定义项不与QQML应用程序引擎一起工作,qt,qml,qwidget,qqmlapplicationengine,Qt,Qml,Qwidget,Qqmlapplicationengine,我用QML制作了一个手风琴,当我尝试在QML项目中使用它时,它工作得很好 但是我需要将它集成到QWidget项目中,所以我尝试使用QQmlApplicationEngine来显示它。但是当我这样做时,什么都不起作用,只在窗口中打印项目的名称 这是我的档案: import QtQuick 2.4 import QtQuick.Layouts 1.0 Item { default property var contentItem: null property string titl

我用QML制作了一个手风琴,当我尝试在QML项目中使用它时,它工作得很好

但是我需要将它集成到QWidget项目中,所以我尝试使用QQmlApplicationEngine来显示它。但是当我这样做时,什么都不起作用,只在窗口中打印项目的名称

这是我的档案:

import QtQuick 2.4
import QtQuick.Layouts 1.0

Item {
    default property var contentItem: null
    property string title: "panel"
    id: root
    height: 30
    Layout.fillHeight: current
    property bool current: false
    ColumnLayout {

        anchors.fill: parent
        spacing: 0
        Rectangle {
            Drag.active: dragArea.drag.active
            id: bar
            Layout.fillWidth: true
            height: 30
            color:  root.current ? "#81BEF7" : "#CEECF5"
            Text {
                anchors.fill: parent
                anchors.margins: 10
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                text: root.title
            }
            Text {
                anchors{
                    right: parent.right
                    top: parent.top
                    bottom: parent.bottom
                    margins: 10
                }
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                text: "^"
                rotation: root.current ? "180" : 0
            }
            MouseArea {
                id: dragArea
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                drag.axis: Drag.YAxis

                drag.target: root

                onReleased: {
                    console.log("release !")
                    root.Drag.drop()
                }
                onClicked: {


                    if (root.parent.current !== root) {
                        console.log('test');
                       // if( (root.parent.currentItem !== null) )
                         // root.parent.currentItem.current = false;
                        root.current = !root.current;

                        root.parent.currentItem = root;
                        if(current) {
                            root.height = 300
                        }
                        else {
                            root.height = 30
                        }
                    }

                }
            }
        }
        Rectangle {
            id: container
            Layout.fillWidth: true
            anchors.top: bar.bottom
            implicitHeight: root.height - bar.height
            clip: true
            Behavior on implicitHeight {
                PropertyAnimation { duration: 100 }
            }
        }
        Component.onCompleted: {
            if(root.contentItem !== null)
               root.contentItem.parent = container;
        }
    }
}
PanelItem.qml

main.qml

这就是我如何使用QQmlApplicationEngine的方法

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

    QQmlApplicationEngine engine("main.qml");

    return a.exec();
}
有人知道为什么吗


非常感谢

当你说什么都不管用时,到底发生了什么?顺便说一句,如果你想结合QWidget和QML,你应该使用,而不是
QQmlApplicationEngine
。它是一个手风琴项目。所以它的工作原理是。但是如果我使用
QQmlApplicationEngine
,我将只看到带有矩形名称的白色矩形。但是我的手风琴没有任何功能可以使用(例如,拖放功能等)。我已经尝试使用
QQuickWidget
,但显示了此错误:
QQuickWidget只支持加载从QQuickItem派生的根对象。如果您的示例使用QML 2(例如qmlscene),并且加载的.QML文件具有“import QtQuick 1.0”或“import Qt 4.7”,则会发生此错误。要加载带有“import QtQuick 1.0”或“import Qt 4.7”的文件,请在Qt Quick 1模块中使用QdeCrativeView类。
这没有意义,因为我没有使用
QtQuick 1.0
Qt 4.7
至于您的问题,您没有定义PanelItem根项目的大小<代码>宽度根本没有定义,因此它是0。应该定义高度,或者使用
height
属性,或者使用
Layout.fillHeight
,而不是两者。但是,在您的情况下,
Layout.fillHeight
是不必要的,因为项目不在布局中。所以只要定义根项目的大小,您的问题就会得到解决。
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QQmlApplicationEngine engine("main.qml");

    return a.exec();
}