【问题标题】:Qt5 android applicationQt5 安卓应用
【发布时间】:2014-11-05 12:57:10
【问题描述】:

在我的 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)

有谁知道我做错了什么?

【问题讨论】:

    标签: android qt qt5


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多