[英]QObject from C++ to QML to QML to C++ (in a list)
My idea is to have a bunch of instances of a QObject drived class in a list (created in C++). 我的想法是在列表中创建一堆QObject驱动类(用C ++创建)。 This list is then passed to QML and each entry can be viewd by a separate QML Object.
然后将此列表传递给QML,并且可以通过单独的QML对象查看每个条目。 Now I want to be able to pass a specific instance back to C++ (eg when clicked).
现在我希望能够将特定实例传递回C ++(例如,单击时)。
Here is some code: 这是一些代码:
QObject derived class QObject派生类
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Data(std::string n):_name(n){};
QString name(){return QString::fromStdString(_name);};
signals:
void nameChanged();
private:
std::string _name;
}
Controller (creating list and receiving selected instance) 控制器(创建列表并接收所选实例)
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<Data> list READ list NOTIFY listChanged)
Controller()
{
_list.append(new Data("data 1");
_list.append(new Data("data 2");
_list.append(new Data("data 3");
};
QQmlListProperty<Data> list() // <--- provide data to QML
{
return QQmlListProperty<Grammar>(this, _list);
};
void takeThisOne(Data* d)// <--- receive selected instance from QML
{
//do something with d
}
signals:
void listChanged();
private:
QList<Data*> _list;
}
QML main (displaying Data list) QML main(显示数据列表)
ApplicationWindow
{
id: mainWindowContainer
width: 800
height: 500
ListView
{
id: dataList
delegate: Rectangle{
height: 10
width: 100
Text{text: name}
}
model: controller.list // <-- what data type are the list items here?
}
Button
{
id: btnOpen
text: "open selected Data in the DataViewer"
onClicked{
// what data type is dataList.currentItem and dataList.currentItem.modelData?
var dataViewer = Qt.createComponent("DataViewer.qml").createObject(mainWindowContainer, {data: dataList.currentItem.modelData});
dataViewer.show()}
}
}
QML DataViewer (displaying data and returning it to the controller) QML DataViewer(显示数据并将其返回给控制器)
Window
{
height: 400
width: 800
property variant data // <--- tried 'property Data data', but did not work
TextArea
{
text: data.name
}
Button
{
id: btnReturn
text: "return to controller"
onClicked: {controller.takeThisOne(data)} // <--- does not work
}
}
I hope this example code is understandable. 我希望这个示例代码是可以理解的。 Thanks for helping!
谢谢你的帮助!
EDIT: 编辑:
I'm doing qmlRegisterType<Data>()
in the main. 我正在主要做
qmlRegisterType<Data>()
。 Also tried qmlRegisterType<Data>("stuff", 1, 0, "Data")
and importing stuff 1.0
into DataViewer. 还尝试了
qmlRegisterType<Data>("stuff", 1, 0, "Data")
并将stuff 1.0
导入DataViewer。 The problem is, that I don't know which data type my Data is at different points: 问题是,我不知道我的数据在不同点上的数据类型:
Controller: list of Data*
QML main : list of ???
dataList.currentItem = ???
dataList.currentItem.modelData = ???
DataViewer: variant or Data (according to property type, but Data does not work)
Controller: obviously not Data* as hoped, but what else?
I finally figured out how to do this! 我终于想出了如何做到这一点! The trick is to use
诀窍是使用
{data: dataList.model[dataList.currentIndex]}
instead of {data: dataList.currentItem]}
or {data: dataList.currentItem.modelData]}
in the main.qml. 而不是
{data: dataList.currentItem]}
的{data: dataList.currentItem]}
或{data: dataList.currentItem.modelData]}
。 Though I still don't know what data types are used and why currentItem seems to use a different data type than model[dataList.currentIndex], this works perfectly! 虽然我仍然不知道使用了什么数据类型以及为什么currentItem似乎使用与模型[dataList.currentIndex]不同的数据类型,但这非常有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.