Qt从角色快速返回对象

时间:2016-02-26 20:47:42

标签: qt c++11 qml qt-quick

我正在尝试获得一个返回对象的角色。当我尝试访问display.blockNumber和display.time

时,我遇到了未定义的错误

这是我的代码

blockdisplay.h:

#ifndef BLOCKDISPLAY_H
#define BLOCKDISPLAY_H

#include <QMetaType>

class BlockDisplay
{
public:
    BlockDisplay();
    BlockDisplay(int blocknum, long time);
    BlockDisplay(const BlockDisplay &other);
    ~BlockDisplay();

    int blockNumber() const;
    long time() const;

private:
    int m_blocknumber;
    long m_time;
};

Q_DECLARE_METATYPE(BlockDisplay)

#endif // BLOCKDISPLAY_H

blockdisplay.cpp:

#include "blockdisplay.h"

BlockDisplay::BlockDisplay() {

}

BlockDisplay::BlockDisplay(int blocknum, long time) {
    this->m_blocknumber = blocknum;
    this->m_time = time;
}

BlockDisplay::BlockDisplay(const BlockDisplay &other) {
    this->m_blocknumber = other.blockNumber();
    this->m_time = other.time();
}

BlockDisplay::~BlockDisplay() {

}

int BlockDisplay::blockNumber() const {
    return this->m_blocknumber;
}

long BlockDisplay::time() const {
    return this->m_time;
}

modelclass.h:

#ifndef MODELCLASS_H
#define MODELCLASS_H

#include <QObject>
#include <QAbstractListModel>
#include <QStringListModel>

#include <blockchain.h>

class ModelClass : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(qint32 blockheight READ blockheight)

protected:
    Blockchain bc{};
    int first;
public:
    ModelClass();
    qint32 blockheight();
    void init();

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
};

#endif // MODELCLASS_H

modelclass.cpp:

#include "modelclass.h"

#include <string.h>
#include <qdebug>

#include "blockdisplay.h"

using namespace std;

ModelClass::ModelClass()
{

}

void ModelClass::init() {
    bc.init();
    if ( !bc.Valid() )
        qDebug() << "invalid";
    else {
        bc.SeekToFirst();
        bc.Next();
        if ( !bc.Valid() )
            qDebug() << "invalid";
        else
            first = bc.GetCurrentBlock().signedhead().head().num();
    }

    //setProperty("blockheight",bc.GetBlockHeight());
}

qint32 ModelClass::blockheight() {
    return bc.GetBlockHeight();
}

int ModelClass::rowCount(const QModelIndex &parent) const {
    //qInfo() << " 0test " << bc.GetBlockHeight();

    return bc.GetBlockHeight() - first;
}

QVariant ModelClass::data(const QModelIndex & index, int role) const {
    qInfo() << " 1test " << index;
    int row = bc.GetBlockHeight() - index.row();// + 1 + first;
    if (index.isValid()) {

        bc.Seek(row);

        if (bc.Valid()) {
            if (role == Qt::DisplayRole) {
                int blocknum = bc.GetCurrentBlock().signedhead().head().num();
                long timestamp = bc.GetCurrentBlock().signedhead().head().timestamp();

                BlockDisplay dsply{blocknum, timestamp};
                QVariant var = QVariant::fromValue(dsply);
                return var;
            }
        }
    }

    return QVariant();
}
来自block.qml的

片段:

Component {
    id: gridComp
    Row {
        Text {
            text: display.blockNumber + " "

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    list.currentIndex = index;
                    ld.setSource("detail.qml")
                }
            }
        }
        Text {
            text: display.time + " "
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为Q_DECLARE_METATYPE (BlockDisplay)是不够的。如果您想在QML中使用其属性,则必须使用Q_PROPERTY宏来创建properties,就像您对ModelClass所做的那样。您可能还需要致电qRegisterMetaType

  

添加Q_DECLARE_METATYPE()使所有基于模板的函数都知道类型,包括QVariant。请注意,如果您打算在排队信号和插槽连接中使用类型,或者在QObject的属性系统中使用类型,则还必须调用qRegisterMetaType(),因为名称是在运行时解析的。