PDA

View Full Version : Connecting signal and slot fails in Symbian emulator



finngruwier
7th August 2011, 09:57
Im am trying to make a very basic Qt application that will use C++ for the data model and QML for the GUI. To my surprise it is not easy to find a good tutorial for this, so it's very much "trial and error". This is the code I've got so far:

main.cpp:


#include <QtDeclarative>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("qml/myapp/main.qml"));
view.show();

QObject *root = view.rootObject();

QObject::connect(root, SIGNAL(quit()), &app, SLOT(quit()));

return app.exec();
}

main.qml:


import QtQuick 1.0

Rectangle {
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
signal quit()
MouseArea {
anchors.fill: parent
onClicked: {
quit();
}
}
}



What it does is very simple: it shows a rectangle with the text "Hello World", and when you click in the rectangle the program quits. I know that this is exactly the same as the default app that Qt Creator creates when you choose to make a new "Qt Quick Application", but my app does it another and (I think) generally more useful way as I don't just start a QML program that sort of lives its own life but actually establish a connection between the C++ layer and the QML layer (with the signal/slot connection).

Now to the problem. This runs without errors for the Desktop target. But if I choose the Qt Simulator as target it still runs, but I get this message in the console:

Object::connect: No such signal QDeclarativeRectangle::quit()

When I click the rectangle the app doesn't quit, and the console says:

Signal QDeclarativeEngine::quit() emitted, but no receivers connected to handle it.

Can someone please correct my code?

Zlatomir
7th August 2011, 11:40
***I didn't tried your code, but the problem is that there is no signal declared.

i don't know QML but i think you should declare the signal, see this (http://doc.qt.nokia.com/4.7-snapshot/qmlevents.html) documentation page and also search the documentation - you should find references about what you want (or at least i think what you want makes sense - the QML isn't there to replace C++, it's just for quick UI "bling-bling" and it should be easy to connect with a C++ layer)

LE: i saw the declared signal after i posted - it's strange that it won't work :o

finngruwier
7th August 2011, 11:50
I have investigated this problem a little more myself, and it turns out that it's not my code that's bad. The problem is that Qt Creator doesn't put a new copy of main.qml in the build directory when I build for the emulator. When I took an updated copy of main.qml and placed it in the build directory it worked. So I guess the problem is in the configuration of my project.

Added after 4 minutes:

"or at least i think what you want makes sense - the QML isn't there to replace C++, it's just for quick UI "bling-bling" and it should be easy to connect with a C++ layer"

- Exactly! :)