posts - 319, comments - 22, trackbacks - 0, articles - 11
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

QML与c++交互学习笔记(五) 导出组合类

Posted on 2011-08-04 21:30 RTY 阅读(476) 评论(0)  编辑 收藏 引用 所属分类: 转载随笔QML

1.导出Person类,并且一个PersonGroup类,PersonGroup类是Person的一个组


2.具体导出过程

1.通过属性来实现,具体的请看代码


3.具体代码



// person.h
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
#include <QDeclarativeListProperty>
#include <QList>
class Person : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ getName WRITE setName NOTIFY sendNameChange)
    Q_PROPERTY(int age READ getAge WRITE setAge NOTIFY sendAgeChange)
public:
    explicit Person(QObject *parent = 0);
    QString getName(void) const;
    void setName(const QString& name);
    int getAge(void);
    void setAge(int age);
signals:
    void sendNameChange(void);
    void sendAgeChange(void);
private:
    QString     m_Name;
    int         m_Age;
};
class PersonGroup : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<Person> members READ members)
public:
    explicit PersonGroup(QObject *parent = 0);
    QDeclarativeListProperty<Person> members(void);
    Q_INVOKABLE int membersCount(void) const;
    Q_INVOKABLE Person *member(int index) const;
private:
    QList<Person*> m_MemberList;
};
#endif // PERSON_H


// person.cpp
#include "person.h"
//---------------------------------
//
Person::Person(QObject *parent) :
    QObject(parent), m_Name("unknow"), m_Age(0)
{
}
//---------------------------------
//
QString Person::getName(void) const
{
    return m_Name;
}
//---------------------------------
//
void Person::setName(const QString& name)
{
    m_Name = name;
    emit sendNameChange();
}
//---------------------------------
//
int Person::getAge(void)
{
    return m_Age;
}
//---------------------------------
//
void Person::setAge(int age)
{
    m_Age = age;
    emit sendAgeChange();
}
//---------------------------------
//
PersonGroup::PersonGroup(QObject *parent)
    :QObject(parent)
{
}
//---------------------------------
//
QDeclarativeListProperty<Person> PersonGroup::members(void)
{
    return QDeclarativeListProperty<Person>(this, m_MemberList);
}
//---------------------------------
//
int PersonGroup::membersCount() const
{
    return m_MemberList.size();
}
//---------------------------------
//
Person *PersonGroup::member(int index) const
{
    return m_MemberList.at(index);
}


// main.cpp
#include <QtGui/QApplication>
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeComponent>
#include "person.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qmlRegisterType<Person>("People",1,0,"Person");
    //qmlRegisterType<Person>();
    qmlRegisterType<PersonGroup>("People",1,0,"PersonGroup");
    QDeclarativeView qmlView;
    qmlView.setSource(QUrl::fromLocalFile("../UICtest/UICtest.qml"));
    qmlView.show();
    return a.exec();
}


// UICtest.qml
import Qt 4.7
import People 1.0 //如果是qmlRegisterType<Person>(); 导出就可以注释这条
Rectangle {
    width: 640
    height: 480
    property int pgcurIndex: 0;
    PersonGroup{
        id: group;
        members: [
            Person { name: "A"; age: 20},
            Person { name: "B"; age: 21},
            Person { name: "C"; age: 22},
            Person { name: "D"; age: 23},
            Person { name: "E"; age: 24}
        ]
    }
    // 显示这份工作的一些信息
    Rectangle{
        x: 100; y: 100;
        width: 100; height: 100;
        Text { id: text1;  text: ""}
        Text { id: text2;  y: 20; text: ""}
        Text { id: text3;  y: 40; text: ""}
    }
    MouseArea{
        anchors.fill: parent;
        onClicked:{
            //if (pgcurIndex < group.membersCount() - 1){ // 这里两种方法都可以
            if (pgcurIndex < group.members.length - 1){
                pgcurIndex++;
            }else{
                pgcurIndex = 0;
            }
            // 显示信息
            text1.text = "PersonGroup index: " + pgcurIndex;
            var person = group.member(pgcurIndex);
            text2.text = "Person name: " + person.name;
            text3.text = "Person age: "  + person.age;
        }
    }
}


说明

这里导出了两个类Person, PersonGroup, PersonGroup保存来一个Person的组,

我们通过导出的函数来调用类面的成员,获取成员的信息.


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理