开发者

Connecting qml-signals to Qt

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 v开发者_运维问答alid pointer. Where am I going wrong?

Thanks


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

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


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.


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


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.)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜