- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的想法是在一个列表中有一堆 QObject 驱动类的实例(用 C++ 创建)。然后将此列表传递给 QML,每个条目都可以通过单独的 QML 对象查看。现在我希望能够将特定实例传递回 C++(例如,单击时)。
这是一些代码:
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 (创建列表和接收选择的实例)
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(显示数据列表)
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(显示数据并返回给 Controller )
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
}
}
我希望这个示例代码是可以理解的。感谢您的帮助!
编辑:
我在做qmlRegisterType<Data>()
在主要。也试过qmlRegisterType<Data>("stuff", 1, 0, "Data")
并导入 stuff 1.0
进入数据查看器。问题是,我不知道我的数据在不同点是哪种数据类型:
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?
最佳答案
我终于知道怎么做了!诀窍是使用
{数据:dataList.model[dataList.currentIndex]}
而不是 main.qml 中的 {data: dataList.currentItem]}
或 {data: dataList.currentItem.modelData]}
。虽然我仍然不知道使用了哪些数据类型以及为什么 currentItem 似乎使用了与模型 [dataList.currentIndex] 不同的数据类型,但它工作得很好!
关于c++ - QObject从C++到QML到QML到C++(在列表中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997742/
以下是我遇到的编译错误: /usr/lib/qt-3.3/include/qobject.h: In copy constructor Product::Product(const Product&)
class CHIProjectData : public QObject { public: CHIProjectData(); CHIProjectData(QMap aProje
这个问题在这里已经有了答案: Could I have copy constructor for subclass of QObject? (3 个答案) 关闭 7 年前。 我收到错误: C:\Qt
将一个项目从 Qt4 迁移到 Qt5 我得到了这个错误,我已经研究过,显然你不能从 QObject 创建一个派生类的复制构造函数(这太不可思议了,因为这段代码不是我的,它应该在以前的版本中编译).复制
我试图返回一个派生自 QObject 的类,但出现以下错误 Error : 'QObject::QObject' : cannot access private member declared in
我正在编写一些派生自 QObject 的 Qt 类,它看起来像: class A : public QObject { Q_OBJECT public: A() : QObject() {}
考虑以下代码: #include class A : public QObject { Q_OBJECT public: A(QObject* parent = 0)
我似乎需要 QObject 类型的 p 实例,我已经扩展了 QObject 并在 fileprocessor.h 中定义了关键字 Q_OBJECT,我不确定我还能做什么。 -文件处理器.h #ifnd
我有一个继承 QObject 的类 BatchItem,还有几个继承自 BatchItem 的类: #ifndef BATCHITEM_H #define BATCHITEM_H #include
这是代码: void invokeQMLFunction2Arg(QObject * object, QString func, QVariant p1, QVariant p2) { QMe
我读过 documentation对于 QObject::connect(对于 Qt 5.4),但我对重载有疑问 QMetaObject::Connection QObject::connect(co
我最近一直在使用QTCreator,我爱上了ATM。不幸的是,我想将它与 Python 一起使用,但我遇到了问题。我遇到的最大问题是发现我的应用程序上下文的子级返回 None。 main.py imp
使用 #include 有什么区别吗?和 #include ? 这两个似乎都有效,所以必须有一些原因来说明为什么 #include是首选吗? 最佳答案 任何不带 .h 扩展名的标准 Qt 头文件都保证
我写的Python代码看起来像这样: class Regularblock(QGraphicsItem): def __init__(self): super(QGraphic
Qt 框架有一个 signal for all QObjects which is emmited before destruction那个QObject。此事件可用于在它指向的对象被销毁时自动清空一
我想创建 SerialPort 类,它可以自动接收消息,然后发出信号。 但是当我编译它时显示错误信息: error: 'QObject' is an ambiguous base of 'Serial
(C++/Qt) 我有一个指向 QObject 的智能指针。让我们说一个 QWeakPointer。由于某些外部原因(可能发生在另一个对象中或由于某个事件),指向的对象可能会被销毁。因为我有一个智能指
QObject 析构函数的 Qt 引用说: 进出该对象的所有信号都会自动断开,该对象的任何未决发布事件都会从事件队列中删除。但是,使用 deleteLater() 通常比删除更安全直接一个 QObje
您好,我需要从后台进行一些套接字通信,我为此使用了QtConcurrent::run,但给了我警告。 QObject: Cannot create children for a parent that
如何打破 QObject 的父子所有权?似乎不再有明确的方法来做到这一点。打电话就够了 QObject::setParent(NULL) 最佳答案 你是对的。制作 QObject一个孤儿,简单地做 /
我是一名优秀的程序员,十分优秀!