Skip to content

Instantly share code, notes, and snippets.

@michalkozminski
Created December 2, 2012 22:58
Show Gist options
  • Save michalkozminski/4191469 to your computer and use it in GitHub Desktop.
Save michalkozminski/4191469 to your computer and use it in GitHub Desktop.
Simple C++ to qml
/*
* GpsInfo.cpp
*
* Created on: 01-12-2012
* Author: michal
*/
#include "GpsInfo.h"
#include <QDebug>
GpsInfo::GpsInfo(QObject* parent):
QObject(parent), m_positionSource(QGeoPositionInfoSource::createDefaultSource(this)){
if(m_positionSource){
connect(m_positionSource, SIGNAL(positionUpdated(const QGeoPositionInfo &)), this, SLOT(positionUpdated(const QGeoPositionInfo &)));
connect(m_positionSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
}
m_positionSource->setUpdateInterval(100);
m_positionSource->requestUpdate(1200);
}
bool GpsInfo::start(){
return true;
}
void GpsInfo::positionUpdated(const QGeoPositionInfo& pos) {
m_verticalSpeed = QString::number(pos.attribute(QGeoPositionInfo::VerticalSpeed));
qDebug() << "aktualizacja";
}
void GpsInfo::updateTimeout() {
qDebug() << "timeout";
}
GpsInfo::~GpsInfo() {
// TODO Auto-generated destructor stub
}
/*
* GpsInfo.h
*
* Created on: 01-12-2012
* Author: michal
*/
#ifndef GPSINFO_H_
#define GPSINFO_H_
#include <QtLocationSubset/QGeoPositionInfo>
#include <QtLocationSubset/QGeoPositionInfoSource>
#include <QtLocationSubset/QGeoSatelliteInfo>
#include <QtLocationSubset/QGeoSatelliteInfoSource>
#include <QtCore/QObject>
using namespace QtMobilitySubset;
class GpsInfo: public QObject {
Q_OBJECT
private:
QGeoPositionInfoSource *m_positionSource;
QString m_verticalSpeed;
private Q_SLOTS:
// This slot is invoked whenever new location information are retrieved
void positionUpdated(const QGeoPositionInfo & pos);
void updateTimeout();
public:
GpsInfo(QObject* parent);
bool start();
virtual ~GpsInfo();
};
#endif /* GPSINFO_H_ */
// Tabbed pane project template
#include "GpsSample.hpp"
#include "GpsInfo.h"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
using namespace bb::cascades;
GpsSample::GpsSample(bb::cascades::Application *app)
: QObject(app)
{
// create scene document from main.qml asset
// set parent to created document to ensure it exists for the whole application lifetime
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
//add gps Status
GpsInfo info = new GpsInfo(0);
qml->setContextProperty("_gpsInfo", &info);
// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);
}
// Tabbed pane project template
#ifndef GpsSample_HPP_
#define GpsSample_HPP_
#include <QObject>
namespace bb { namespace cascades { class Application; }}
/*!
* @brief Application pane object
*
*Use this object to create and init app UI, to create context objects, to register the new meta types etc.
*/
class GpsSample : public QObject
{
Q_OBJECT
public:
GpsSample(bb::cascades::Application *app);
virtual ~GpsSample() {}
};
#endif /* GpsSample_HPP_ */
// Tabbed pane project template
#include "GpsSample.hpp"
#include <bb/cascades/Application>
#include <QLocale>
#include <QTranslator>
#include <Qt/qdeclarativedebug.h>
//for debug
#include <stdio.h>
using namespace bb::cascades;
//send message form qDebug to console
void myMessageOutput(QtMsgType type, const char* msg){
fprintf(stdout, "%s\n", msg);
fflush(stdout);
}
Q_DECL_EXPORT int main(int argc, char **argv)
{
//init qDebug
qInstallMsgHandler(myMessageOutput);
// this is where the server is started etc
Application app(argc, argv);
// localization support
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString( "gpsSample_%1" ).arg( locale_string );
if (translator.load(filename, "app/native/qm")) {
app.installTranslator( &translator );
}
// create the application pane object to init UI etc.
new GpsSample(&app);
// we complete the transaction started in the app constructor and start the client event loop here
return Application::exec();
// when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}
// Tabbed Pane project template
import bb.cascades 1.0
NavigationPane {
Page{
actions: [
ActionItem{
title: qsTr("Start Updates")
ActionBar.placement: ActionBarPlacement.OnBar
onTriggered: {
console.log("test")
_gpsInfo.start()
}
}
]
Container {
layout: StackLayout {
}
Button {
id: button
text: "button"
}
SpeedMeter{
}
Label {
id: label
text: "Loading"
}
}
attachedObjects: [
ComponentDefinition {
id: speedMeter
source: "speedMeter.qml"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment