Hi, I'm trying to use a qml-grid view in my code. I'm trying to couple it with my C++ code. I've dynamically created a list view model and passed across the qml file. It works fine. However, I'm facing trouble when I want to connect a Qml signal to Qt/c++ code. I've handled mouseArea in my Qml-rectangle and emitting a signal from there.

I'm trying to connect to the signal as follows:

Qt Code:
  1. QDeclarativeView *pQMLContainer = NULL;
  2. TempWidget *pTemp = new TempWidget();
  3. pQMLContainer = new QDeclarativeView(pTemp);
  4. pQMLContainer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
  5. pQMLContainer->rootContext()->setContextProperty("imgModel", createModel() );
  6. pQMLContainer->setSource(QUrl("..Temp/qml/gridview-example.qml"));
  7. QObject *rootObject = dynamic_cast<QObject*>pQMLContainer->rootObject();
  8. QObject::connect(rootObject, SIGNAL(keyPressed()), pTemp, SLOT(onKeyPressed()));
To copy to clipboard, switch view to plain text mode 

When the connect statement runs, I get an error: cannot connect to "null" object. On debugging, I found I could never get "rootObject" as a valid pointer. Where am I going wrong?

Thanks