PDA

View Full Version : Connecting Qml signals to Qt



mots_g
10th May 2011, 10:48
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:


QDeclarativeView *pQMLContainer = NULL;
TempWidget *pTemp = new TempWidget();
pQMLContainer = new QDeclarativeView(pTemp);
pQMLContainer->setResizeMode(QDeclarativeView::SizeRootObjectToVi ew);
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()));

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

MarekR22
10th May 2011, 11:17
content is not shown, so most probably Qt optimization prevent creation of any object in QDeclarativeView since it is not needed until it is shown.

Another thing why cast to QObject? It is base class for all QGraphicsObjects.
And signal is wrong for sure! Where are the arguments?

mots_g
10th May 2011, 12:01
Hi Marek,
Thanks for your suggesstions.

However, I tried to show the QDeclarativeView and then call connect. It still gave me the same error.
Also, if I try to get QGraphicsObject from QDeclarativeView::rootObject, I still get a null pointer.
The signals and slots do not have any parameters(void).

Thanks,
Mots

content is not shown, so most probably Qt optimization prevent creation of any object in QDeclarativeView since it is not needed until it is shown.

Another thing why cast to QObject? It is base class for all QGraphicsObjects.
And signal is wrong for sure! Where are the arguments?

MarekR22
10th May 2011, 12:21
Take a look at http://doc.qt.nokia.com/latest/qtbinding.html there is section "Receiving signals".
C++ code there is similar to what you have presented so apparently your QML code must be wrong.

Edit:
PS. Isn't your URL wrong "..Temp/qml/gridview-example.qml"? Or is this just mistype in posting this message?

high_flyer
10th May 2011, 14:03
If OP can see his QML UI, then the url must be right, and also it must have a root object...

mots_g
12th May 2011, 12:37
@MarekR22: Yes the URL for qml was correct as I could see my grid-view and all its items.

I found the solution and it now works fine (I'm getting all the signals).
The change I made was I added the qml to my 'qrc' and gave the path relative to 'qrc'. I'm not sure why it made a difference even though earlier, it could load the 'qml' from the relative path to the binary.

Thanks everyone for your help!
Mots