vendredi 26 février 2016

Qt Quick returning objects from roles

I'm trying to get a role to return an object. I'm running into undefined errors when I try to access display.blockNumber and display.time

here's my code

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();
}

snippet from 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 + " "
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire