我想在我的代码中使用qml网格视图。我试图将它与我的C++代码结合起来。 我已经动态创建了一个列表视图模型,并通过qml文件传递。它工作正常。 但是,当我想要将Qml信号连接到Qt/C++代码时,我遇到了麻烦。我在我的Qml-rectangle中处理了mouseArea并从那里发出信号。qml信号连接到Qt
我试图连接到信号如下:
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'。 – laalto 2011-05-10 10:39:30