ListView无法从另一个qml文件加载委托

问题描述 投票:0回答:1

我想将ListViewDelegate.qml用作我Kos.qml上列表视图项目的委托。但这给了我这个错误。我也尝试使用加载程序,但似乎没有用,我尝试删除花括号并加载Kos.qml页面,但未呈现列表视图

file:/ngomahyukv2/Kos.qml:55: ListViewDelegate is not a type
File name case mismatch

@ Kos.qml我使用另一个文件PageBackground.qml作为背景

import QtQuick 2.4
import QtQuick.Controls 2.3

PageBackground {
    id: kos
    width: 640
    height: 480

    Text {
        id: element
        x: 6
        y: 20
        width: 24
        height: 32
        color: "#ffffff"
        text: qsTr("<")
        font.strikeout: false
        styleColor: "#ffffff"
        font.underline: false
        font.italic: false
        font.bold: true
        font.pixelSize: 25
        MouseArea {
            id: mouseArea
            anchors.rightMargin: 0
            anchors.bottomMargin: 0
            anchors.leftMargin: 0
            anchors.topMargin: 0
            anchors.fill: parent
            onClicked: kosloader.source = ""
        }

    }

    ListView {
        id: listView
        x: 0
        y: 69
        width: 640
        height: 410
        model: ListModel{
//            need to be in for loop and data from database
           ListElement{
                imagePath : "static/Bisnis-kos-kosan.png"
                kosName : "Kos Name"
                kosAlamat : "Jalan Lorem"
                kosJumlahKamar: "5"
                kosGender : "Laki-laki"
                kosHarga: "7000000"
                kosProfile: "KosSpec.qml"
                ownerContact: "instgram.com"
            }
        }

        delegate: ListViewDelegate { }
   }

    Loader{
        id: kosspec
        visible: false
        source: ""
    }
}

@@ ListViewDelegate.qml

import QtQuick 2.0
import QtQuick.Controls 2.13

Item {
    id: listviewdelegate
    Image {
        id: idthumbnail
        x: 8
        y: 8
        width: 227
        height: 158
        source: imagePath
        fillMode: Image.PreserveAspectCrop
    }

    Text {
        id: idnamakos
        x: 252
        y: 8
        text: kosName
        font.bold: true
        font.family: "Verdana"
        wrapMode: Text.WordWrap
        font.pixelSize: 22
    }

    Text {
        id: idalamat
        x: 251
        y: 40
        width: 301
        height: 17
        text: qsTr("Alamat            : " + kosAlamat)
        wrapMode: Text.WordWrap
        font.pixelSize: 12
        font.family: "Verdana"
    }

    Text {
        id: idjumlahkamar
        x: 252
        y: 63
        width: 240
        height: 14
        text: qsTr("Jumlah Kamar : " + kosJumlahKamar)
        wrapMode: Text.WordWrap
        font.pixelSize: 12
        font.family: "Verdana"
    }

    Text {
        id: idgender
        x: 251
        y: 83
        width: 265
        height: 14
        text: qsTr("Gender            : " + kosGender)
        wrapMode: Text.WordWrap
        font.pixelSize: 12
        font.family: "Verdana"
    }

    Text {
        id: idharga
        x: 251
        y: 103
        width: 373
        height: 14
        text: qsTr("Harga              : " + kosHarga)
        wrapMode: Text.WordWrap
        font.pixelSize: 12
        elide: Text.ElideNone
        font.family: "Verdana"
    }

    Button {
        id: buttonCek
        x: 510
        y: 131
        width: 96
        height: 35
        visible: true
        font.family: "Verdana"
        font.pixelSize: 16
        background: Rectangle{
            color: "#ef3644"
            anchors.fill: parent

        }
        contentItem: Text {
            id: cek
            text: "CEK"
            anchors.fill: parent
            verticalAlignment: Text.AlignVCenter
            font.bold: true
            font.pointSize: 10
            horizontalAlignment: Text.AlignHCenter
            color: "#ffffff"
        }

        MouseArea {
            id: mouseAreaCek
            anchors.fill: parent
            onClicked: {
                kosspec.visible = true
                kosspec.source = kosProfile
            }
        }


    }

    Button {
        id: buttonHubungi
        x: 400
        y: 131
        width: 96
        height: 35
        font.family: "Verdana"
        visible: true
        contentItem: Text {
            id: name1
            color: "#ef3644"
            text: "HUBUNGI"
            anchors.fill: parent
            font.bold: true
            font.pointSize: 10
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
        background: Rectangle {
            color: "#00f1f0f0"
            border.width: 4
            border.color: "#ef3644"
            anchors.fill: parent
        }
        font.pixelSize: 16

        MouseArea {
            id: mouseAreaHubungi
            anchors.fill: parent
            onClicked: {
                Qt.openUrlExternally (ownerContact);
            }
        }
    }

    ToolSeparator {
        id: toolSeparator
        x: 5
        y: 172
        width: 600
        height: 15
        orientation: Qt.Horizontal
    }

}
qt listview delegates qml
1个回答
0
投票

如错误消息所述,这似乎与文件的大小写有关。自定义类型需要以大写字母开头,但还要检查您是否以正确的大小写获得了VD

而且文件是否在同一目录中?您是否有相同的文件名,但并行使用不同的大小写?目录?

我非常有信心,问题出在文件名,或者是干扰文件名的文件名。但是,如果要创建类型模块并以这种方式引用文件,则也可以使用qmldir文件来戳一下:https://doc.qt.io/qt-5/qtqml-modules-qmldir.html

但是首先尝试在文件中找到一些奇怪的东西。

© www.soinside.com 2019 - 2024. All rights reserved.