2016-02-26 70 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:從block.qml

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

片段:

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(),因爲名稱在運行時解析。