【发布时间】:2011-05-10 08:41:19
【问题描述】:
我正在尝试在我的代码中使用 qml-grid 视图。我正在尝试将它与我的 C++ 代码结合起来。 我已经动态创建了一个列表视图模型并通过了 qml 文件。它工作正常。 但是,当我想将 Qml 信号连接到 Qt/c++ 代码时,我遇到了麻烦。我已经在我的 Qml-rectangle 中处理了 mouseArea 并从那里发出信号。
我正在尝试按如下方式连接到信号:
QDeclarativeView *pQMLContainer = NULL;
TempWidget *pTemp = new TempWidget();
pQMLContainer = new QDeclarativeView(pTemp);
pQMLContainer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
pQMLContainer->rootContext()->setContextProperty("imgModel", createModel() );
pQMLContainer->setSource(QUrl("../Temp/qml/gridview-example.qml"));
QObject *rootObject = dynamic_cast<QObject*>pQMLContainer->rootObject();
QObject::connect(rootObject, SIGNAL(keyPressed()), pTemp, SLOT(onKeyPressed()));
当连接语句运行时,我得到一个错误:无法连接到“空”对象。 在调试时,我发现我永远无法将“rootObject”作为有效指针。 我哪里错了?
谢谢
【问题讨论】:
-
不需要
dynamic_cast- 根QGraphicsObject是QObject。