QML Access c++ Models

Alles rund um die Programmierung mit Qt
Antworten
Furby00
Beiträge: 1
Registriert: 28. November 2012 12:32

QML Access c++ Models

Beitrag von Furby00 »

Hi,

ich hoffe ich bin richtig hier. Ich habe vor kurzen mit qml angefangen und dabei recht schnell auf mein erstes Problem gestoßen und zwar dem Datenaustausch zwischen qml und c++.
Ich möchte gerne auf mein C++ ListModel ( erbt von QAbstractListModel) außerhalb eines (QML) ListView zugreifen.
Innerhalb der (QML) ListView funktioniert alles perfekt. Ich habs auch bereits mit qmlRegisterInterface oder/und qRegisterMetaType versucht aber leider ohne Erfolg.
Hier mal mein code (Ich hab auch ein kleine Beispielprogramm angehängt):
main.cpp

Code: Alles auswählen

    ListModel *listModel = new ListModel();
    listModel->add(new Subclass("Class 1", "Class 1.1", "Class 1.2") );

//    qmlRegisterInterface<ListModel>("ListModel");
    qmlRegisterInterface<Parent>("Parent");
    qmlRegisterInterface<Subclass>("Subclass");

     qRegisterMetaType<Parent>("Parent");
     qRegisterMetaType<Parent>("Subclass");

    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("listModel", listModel);

    viewer.setMainQmlFile(QStringLiteral("qml/test/main.qml"));
    viewer.showExpanded();


parent.h (subclass ist genau so aufgebaut und erbt von Parent)

Code: Alles auswählen

class Parent : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString firstname READ getFirstname WRITE setFirstname NOTIFY firstNameChanged )
    Q_PROPERTY(QString lastname READ getLastname WRITE setLastname NOTIFY lastNameChanged)

public:
    explicit Parent(){}
    explicit Parent(const QString &first, const QString &last,QObject *parent = 0);
    Parent( const Parent &p);
    Parent& operator=(const Parent & p);

signals:
    
public slots:
    QString getFirstname() const;
    QString getLastname() const ;

    void setFirstname( const QString &firstname);
    void setLastname( const QString &lastname);

private:
    QString _firstname;
    QString _lastname;

signals:
    void firstNameChanged();
    void lastNameChanged();
    
};

Q_DECLARE_METATYPE( Parent )
main.qml

Code: Alles auswählen

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    Component
    {
        id: decorator
        Rectangle{
            height: 100
            width: 360

            color: "yellow"
            opacity: 0.5

            Text {
             text:  '<b>Firstname:</b> ' + model.firstname1 + '<br>' +
                    '<b>Lastname:</b> ' + model.lastname1 + '<br>' +
                    '<b>SubclassName:</b> ' + model.scublassname1 + '<br>'
            }

            MouseArea {
                anchors.fill: parent;
                onClicked:{
                    listview.currentIndex = index;
                }
            }
        }
    }

    ListView{
        id: listview
       anchors.top: parent.top
       anchors.left: parent.left
       anchors.right: parent.right

        height: 200
        model: listModel
        delegate: decorator
        spacing: 15
        focus: true
        clip: true

    }

    Rectangle{
        color: "blue"
        anchors.top: listview.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        height: 160

        Text{
            anchors.fill: parent
            text: {
                "index =["+listview.currentIndex+"]  " +  listModel.get(listview.currentIndex).firstname
            }
        }
    }
}

Ich hoffe Ihr könnt mir helfen.
Dateianhänge
test.zip
vollständiger code
(19.13 KiB) 163-mal heruntergeladen
Antworten