2
votes

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::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()));

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

5
There's no need for dynamic_cast - the root QGraphicsObject is a QObject.laalto

5 Answers

1
votes

Can you try this ? (it is example code from Qt Docs)

QObject *item = pQMLContainer->rootObject();
QObject::connect(item, SIGNAL(keyPressed()),
                  pTemp, SLOT(onKeyPressed()));
1
votes

The code is pretty much straight:

in .cpp file:

ui->declarativeView->setSource(QUrl("qrc:/Resources/main.qml"));
QGraphicsObject *obj = ui->declarativeView->rootObject();
connect ( obj, SIGNAL(clicked()), this, SLOT(itemClicked()));

and QML File:

import Qt 4.7

Rectangle {
    width: 100
    height: 100
    id: rect

    signal clicked

    Text {
        text: "Hello World"
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            rect.clicked();
        }
    }
}

one more thing, check the location of your qml file, it should be accessible to the binary.

0
votes

Perhaps you should use qobject_cast instead of dynamic_cast? See e.g. question dynamic_cast returns NULL but it shouldn't

0
votes

QGraphicsObject is a QObject so no cast should be required. If your compiler complains, try adding #include <QGraphicsObject>.

Just casting without the compiler knowing the classes is asking for trouble. (Especially as there is multiple inheritance involved.)

0
votes

I could finally get this working. I'm not sure if this is the real solution to the problem, but finally this got it working: I was setting the qml path as a relative path to my working folder. And yes the path was indeed correct, as I could see the qml and its contents. I just happened to change the qml path from relative to the working folder to relative to "qrc" as:

pQMLContainer->setSource(QUrl("qrc:/gridview-example.qml"));
instead of:
pQMLContainer->setSource(QUrl("../Temp/qml/gridview-example.qml"));

and it started working. I'm not sure if I had to add the qml to the qrc (I've just started using qml).

Thanks everyone for your support! Mots