Qt Quick 从角色返回对象
Posted
技术标签:
【中文标题】Qt Quick 从角色返回对象【英文标题】:Qt Quick returning objects from roles 【发布时间】:2016-02-26 20:47:42 【问题描述】:我正在尝试让角色返回一个对象。当我尝试访问 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;
模型类.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 dsplyblocknum, timestamp;
QVariant var = QVariant::fromValue(dsply);
return var;
return QVariant();
来自block.qml的sn-p:
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(),因为名称是在运行时解析的。
【讨论】:
以上是关于Qt Quick 从角色返回对象的主要内容,如果未能解决你的问题,请参考以下文章