QML Object Type is not a type error in QTCreator

2019-04-28 07:10发布

Hi Everyone i am new to QT and i am having trouble loading one qml through another qml Basically i have created a qml MyTabView(MyTabView.qml)

 import QtQuick 2.3
 import QtQuick.Controls 1.2

TabView {
    width: 360
    height: 360

    Component.onCompleted: {
        addTab("Tab 1", tab1)

      addTab("Tab 2", tab2)
    }

    Component {
        id: tab1
        Rectangle {color: "red"}
    }

    Component {
        id: tab2
        Rectangle {color: "blue"}
    }
}

and i am trying to show it through another qml(main.qml) which is in the same directory

import QtQuick 2.3
import QtQuick.Controls 1.2
import "."

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Main")

MyTabView {}

}

but when i try to run my project i get this error

QQmlApplicationEngine failed to load component qrc:/qml/main.qml:11 TabView is not a type

Please note that i have M Caps in MyTabView.qml and that MyTabView.qml and main.qml are in the same directory.

Can someone point me what mistake i am doing ? One thing i want to point is that when i replace all the code of MyTabView.qml instead of MyTabView {} inside main.qml,the program does not give any error and runs correctly. Thanks in advance

3条回答
▲ chillily
2楼-- · 2019-04-28 07:51

You should rename your "TabView.qml" to something like "MyTabView.qml".

Because of that import

import "."

you have conflict of TabView from "QtQuick.Controls 1.2" and local folder "."

查看更多
萌系小妹纸
3楼-- · 2019-04-28 08:05

Have you added the file to your Resources ?
Adding your MyTabView.qml to your project in the same directory of main.qml is not sufficient.
You have to put your QML file in the Resources (probably main.qrc/qml/) in order to have it deployed.
The editor of Qt Creator does not need this inclusion in order to find your type, therefore it displays no error.

查看更多
放荡不羁爱自由
4楼-- · 2019-04-28 08:08

I had a similar problem.

qrc:AGview.qml:8:15: AGraph is not a type

I solved it: my original code (in my main.cpp):

view.setSource(QUrl("qrc:AGview.qml"));

the working one:

view.setSource(QUrl("qrc:/AGview.qml"));

I think without the slash it don't search in the actual folder.

查看更多
登录 后发表回答