在Qt中将自定义对象附加到QStandardItem [英] Attach custom object to QStandardItem in Qt
问题描述
我使用 QTreeView
向用户显示一些数据。我想要的是将一个实际的对象附加到使用 QStandardItem
表示的每个节点。
QStandardItem
:
QStandardItem * child = new QStandardItem
child-> setFlags(child-> flags()&〜Qt :: ItemIsEditable);
child-> setData(QVariant(QVariant :: UserType,i),Qt :: UserRole + 10);
在UI中点击时访问实际对象:
void MyOtherClass :: handleTreeViewSelectionChanged(const QModelIndex& i)
{
MyClass * o = i.data(Qt :: UserRole + 10).value< MyClass *>();
//用o
进行其他操作
NULL
任何人都知道如何处理这样的要求?
我发现网络上没有什么有用的。
任何帮助将非常感激。
要存储您的项目在QStandardItem你需要确保你' ve使用QMetaType注册了您的类型。例如,您可能有以下定义:
class MyType
{
public:
MyType():m_data(0){}
int someMethod()const {return m_data; }
private:
int m_data;
};
Q_DECLARE_METATYPE(MyType *); //注意我已经将POINTER声明为你的类型
一个QVariant像这样:
MyType * object = new MyType;
QVariant variant;
variant.setValue(object);
为您的类型注册了正确的元类型,现在可以使用QStandardItems: / p>
MyType * object = new MyType;
QStandardItemModel模型;
QStandardItem * parentItem = model.invisibleRootItem();
QStandardItem * item = new QStandardItem;
item-> setData(QVariant :: fromValue(myType)); //默认为Qt :: UserRole + 1
parentItem-> appendRow(item);
然后提取:
void MyOtherClass :: handleTreeViewSelectionChanged(const QModelIndex& i)
{
MyType * o = i.data(Qt :: UserRole + 1).value& >();
//用o
做其他事情}
I'm using QTreeView
to show some data to the user. What I want is to attach an actual object to each node represented using QStandardItem
.
To save the object reference into QStandardItem
:
QStandardItem *child = new QStandardItem(s);
child->setFlags(child->flags() & ~Qt::ItemIsEditable);
child->setData(QVariant(QVariant::UserType, i), Qt::UserRole + 10);
To access the actual object when it is clicked in the UI:
void MyOtherClass::handleTreeViewSelectionChanged(const QModelIndex &i)
{
MyClass* o = i.data(Qt::UserRole + 10).value<MyClass*>();
// do other stuff with o
}
The above call just returns NULL
.
Anybody knows how to deal with such a requirement?
I found absolutely nothing useful on the web.
Any help would be highly appreciated.
To store your item in the QStandardItem you need to make sure that you've registered your type with QMetaType. For example, you might have this definition:
class MyType
{
public:
MyType() : m_data(0) {}
int someMethod() const { return m_data; }
private:
int m_data;
};
Q_DECLARE_METATYPE(MyType*); // notice that I've declared this for a POINTER to your type
Then you would store it into a QVariant like so:
MyType *object = new MyType;
QVariant variant;
variant.setValue(object);
Given a properly registered metatype for your type, you can now do something like this with your QStandardItems:
MyType *object = new MyType;
QStandardItemModel model;
QStandardItem *parentItem = model.invisibleRootItem();
QStandardItem *item = new QStandardItem;
item->setData(QVariant::fromValue(myType)); // this defaults to Qt::UserRole + 1
parentItem->appendRow(item);
And then later extract it:
void MyOtherClass::handleTreeViewSelectionChanged(const QModelIndex &i)
{
MyType* o = i.data(Qt::UserRole + 1).value<MyType*>();
// do other stuff with o
}
这篇关于在Qt中将自定义对象附加到QStandardItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!