Qt5 android应用程序

时间:2014-11-05 12:57:11

标签: android qt qt5

在我的Android应用程序中,我想显示一个链接列表。

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

    LinkListModel social_links;
    social_links.get(content_category::social);

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", &social_links);

    view.setSource(QUrl("qrc:main.qml"));
    view.show();

    return app.exec();
}

这是我的.qml文件:

import QtQuick 2.0
import QtQuick.Controls 1.1

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 640
    height: 480

    menuBar: MenuBar {
        Menu {
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    ListView {
        width: 640
        height: 480

        model: myModel
        delegate: Rectangle {
            height: 25
            width: 100
            Text { text: "modelData: " + title }
        }
    }
}

我没有展示我的模型类,因为它对我的问题无关紧要。 当我想在模拟器中运行应用程序时,我收到以下错误消息,应用程序中止:

W/EGL_emulation( 6582): eglSurfaceAttrib not implemented
W/Qt      ( 6582): items\qquickview.cpp:518 (void QQuickViewPrivate::setRootObject(QObject*)): QQuickView only supports loading of root objects that derive from QQuickItem. 
W/Qt      ( 6582): 
W/Qt      ( 6582): If your example is using QML 2, (such as qmlscene) and the .qml file you 
W/Qt      ( 6582): loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur. 
W/Qt      ( 6582): 
W/Qt      ( 6582): To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the 
W/Qt      ( 6582): QDeclarativeView class in the Qt Quick 1 module. 
W/Qt      ( 6582): 
F/libc    ( 6582): Fatal signal 11 (SIGSEGV) at 0x2a43e000 (code=1), thread 6582 (.example.AKBApp)

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

QQuickView仅支持加载源自QQuickItem的根对象。 ApplicationWindow来自QQuickWindow。您应该使用QQmlApplicationEngine代替QQuickView

QApplication app(argc, argv);

LinkListModel social_links;
social_links.get(content_category::social);

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myModel", &social_links);
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();