qobject_cast总是返回NULL?

时间:2016-04-30 06:05:24

标签: qt qml

我正在尝试使用 qobject_cast 从QObject *转换为QSlider *,但是通过调试,变量cObj不是NULL,但是vNumSlider是NULL。< / p>

这是.cpp

中的一些相关摘录
int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QQmlComponent component(&engine,QUrl(QStringLiteral(MAIN_FORM)));
    QObject *obj = component.create();
    QObject *cObj = obj->findChild<QObject*>("vNumSlider");
    QSlider *vNumSlider = qobject_cast<QSlider*>(cObj);
    delete obj;
    return app.exec();
}

和.qml

Slider {
    id: vNumSlider
    objectName: "vNumSlider"
    maximumValue: 130
    x: 85
    y: 12
}

0 个答案:

没有答案