2016-02-26 4 views
0

Я пытаюсь получить роль для возврата объекта. Я бегу в неопределенных ошибок при попытке получить доступ к display.blockNumber и display.timeQt Быстрое возвращение объектов из ролей

вот мой код

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

Я думаю Q_DECLARE_METATYPE(BlockDisplay) недостаточно. Если вы хотите использовать его атрибуты в QML, вы должны использовать макрос Q_PROPERTY для создания properties, как и для ModelClass. Вы также можете позвонить qRegisterMetaType

Добавление Q_DECLARE_METATYPE() делает вид, известный всем шаблонных на основе функций, в том числе QVariant. Обратите внимание: если вы собираетесь использовать тип в очереди сигналов и слотов или в системе свойств QObject, вам также нужно вызвать qRegisterMetaType(), поскольку имена будут разрешены во время выполнения.