c++ - ComboBox初始化错误 : Cannot read property 'constructor' of undefined

标签 c++ qt qml qtquick2 qtquickcontrols

当我尝试初始化 ComboBox 的模型时,弹出奇怪的错误

测试.pro

# Add more folders to ship with the application, here
folder_01.source = qml/androidTest
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

#QMAKE_CXXFLAGS += -std=c++0x
CONFIG   += c++11
QT += qml quick

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp

OTHER_FILES += \
    qml/androidTest/main.qml

主要.cpp

#include <QtGui/QGuiApplication>
#include <QQuickView>

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

    QQuickView view;
    view.setSource(QUrl("/Users/Qt/program/experiment_apps_and_libs/test/qml/test/main.qml"));
    view.show();

    return app.exec();
}

main1.qml

import QtQuick 2.2
import QtQuick.Controls 1.1

Rectangle {
width: 100
height: 62

ListModel{
id: modelA
}

ComboBox{
model: modelA
}

Component.onCompleted: {
modelA.append({"source" : "hhhh"})
}
}

错误信息

file:///C:/Qt/Qt5.2.0/5.2.0/mingw48_32/qml/QtQuick/Controls/ComboBox.qml:496: TypeError: Cannot read property 'constructor' of undefined

我该如何解决这个错误?

编辑 1:

我没有制作内联模型,因为我想将模型的构造和组合框分开。我的英语不好很难解释,这里是一个简单的例子

文本CB

Column{
    id: root    

    function appendUnitModel(units){
        for(var i = 0; i != units.length; ++i){
            unitModel.append({"unit": units[i]});
        }
    }    

    property alias inputText: input.text

    SystemPalette{id: palette}    

    ListModel{
        id: unitModel
    }

    Row{
        spacing: 5

        Text{
            id: input

            color: palette.highlight
            height: root.height / 2
            width: root.width * 0.6
            focus: true
            font.family: "Helvetica"
            font.pixelSize: 16; font.bold: true

            //Behavior on height{ NumberAnimation{duration: 500} }

            MouseArea{
                anchors.fill: parent

                onClicked: {
                    showKeyBoard()
                }
            }
        }

        ComboBox{
            id: unitSelector

            model: unitModel
            editable: true
            height: input.height
            width: root.width - input.width
        }
    }    
}

main2.qml

 TextCB{
    id: inputAndClear

    height: root.height * 0.2
    width: root.width        

    Component.onCompleted: {
        var units = ["meters", "decimeters", "centimeters",
                     "millimeters", "kilometers", "inches",
                     "feet", "yards", "miles", "nautical miles",
                     "cables"]

        inputAndClear.appendUnitModel(units)
    }

}

将模型和 ComboBox 的构造分开,我可以更容易地重用它。

编辑 2: 对于不会使用QtCreator的,这里是命令行

  1. /Users/yyyy/Qt5.2.0/5.2.0/clang_64/bin/qmake -makefile -d test.pro
  2. 制作
  3. cd androidTest.app/Contents/MacOS
  4. lldb测试
  5. 运行

此命令在 OSX 下,您可能需要在不同的操作系统下稍微调整一下(例如:将 lldb 更改为 gdb)

最佳答案

问题是您正在尝试为 ListModelListElement 设置“源”属性,而不是它所期望的“文本”。相应地,如果您更改以下行:

modelA.append({"source" : "hhhh"})

到:

modelA.append({"text" : "hhhh"})

它会起作用的。或者,您也可以将以下行添加到您的 ComboBox 以使您的自定义角色生效:

ComboBox {
    model: modelA
    textRole: "source"
}

详细解释见ComboBox代码:

// No text role set, check whether model has a suitable role
// If 'text' is found, or there's only one role, pick that.

您的代码中还有其他小问题,例如硬编码 Windows 的 qml 路径,如下所示。您可以简单地更改为“main.qml”,或者使用资源系统。

view.setSource(QUrl("/Users/Qt/program/experiment_apps_and_libs/test/qml/test/m‌​ain.qml"));

我个人在本地简单地将其更改为:

view.setSource(QUrl("m‌​ain.qml"));

此外,您似乎为这个实验设置了不必要的 qmake 选项,如下所示:

CONFIG += c++11

QT += qml quick

对于后者,您不需要明确指定qml

关于c++ - ComboBox初始化错误 : Cannot read property 'constructor' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833809/

相关文章:

c++ - 这些是堆栈分配还是堆分配?

c++ - 为什么变量声明和for循环的条件一样好用?

c++ - 使用 C++ vector::insert() 添加到 vector 的末尾

python - 如何将 QTableView 项目方向从垂直更改为水平?

c++ - 共享库 QT 资源

Qt Quick QML MouseArea 按下时自动重复

c++ - 通过右值引用传递并使用 std::move() 返回?

python - 重新实现QAbstractTableModel后TableView没有数据显示在表格中

qt - 如何在 QML 中为 ListView 项目设置替代颜色

qml - 使用状态时收到 'Cannot anchor to a null item'