Sunday, February 27, 2011

More QML learnings

I was trying to figure out how to connect button click to C++. Here is very short how (sorry for bad formatting due to this html which does not like code):
main.cpp:

For this I had these includes:
#include < QtGui/QApplication >
#include < QString >
#include < QDeclarativeEngine >
#include < QDeclarativeView >
#include < QtDeclarative >
#include "qmlapplicationviewer.h"
#include < QDebug >

Then continued with my program specific includes
...

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QmlApplicationViewer viewer;
qmlRegisterType < Metar > ("MetarClasses",1,0,"Metar");
viewer.setOrientation(QmlApplicationViewer::LockPortrait);
viewer.setMainQmlFile(QLatin1String("qml/PilotHelper/main.qml"));
viewer.show();
Metar met;

return app.exec();
}

metar.h (from my unfinished application):

#include < QDeclarativeEngine >
#include < qdir.h >

class Metar : public QObject {
Q_OBJECT
public:

explicit Metar(QObject *parent = 0);
~Metar();
QString readMetar(QString location);

public slots:

void replyFinishedSlot(QNetworkReply *reply);
void retrieveMetarClickedSlot();

private:
QNetworkAccessManager* nam;

};

metar.cpp:

implements clicked function


void Metar::retrieveMetarClickedSlot(){

qDebug () << "Click" << "\n";
QString myreply = readMetar("EFHF");
qDebug () << "METAR=" << myreply << "\n";

}

Then QML:

Imports:

import Qt 4.7
import QtQuick 1.0
import MetarClasses 1.0

Then button that sends click to the metar class:
Metar
{
id: met

}

Button
{
anchors.centerIn: parent
height: 50; width: 400
text: "Retrieve metar"
onClicked:(met.retrieveMetarClickedSlot())
}

(The button is custom button not defined in this snipplet, use your own button or button from meego (follow Kate Alhola's Forum Nokia blog's instructions how to do that).

1 comment:

Anonymous said...

why don't you use the <pre> tag for correct code formatting?