How to write code in C language instead of QML?
Hello,
I want to make program to Nokia phone with using accelerometers. I make new project in Qt Creator. Qt Quick Project->Qt Quick Application. Then Target: Symbian Device, Desktop and Qt Simulator.
But I have code in QML language. I want to write in C language. What to do?
Best regards.
Re: How to write code in C language instead of QML?
Create a Qt mobile application instead of Qt Quick application.
Re: How to write code in C language instead of QML?
Thanks for answer. I want to write a program to N8 mobile phone which would read accelerometer data and show value on screen at Label.
I have this code:
Code:
#include "mainwindow.h"
#include <QtGui/QApplication>
#include <stdio.h>
#define ACCELEROMETER_FILE_N900 "/sys/class/i2c-adapter/i2c-3/3-001d/coord"
class Accelerometer {
int x;
int y;
int z;
public:
Accelerometer() : x(0), y(0), z(0)
{
update();
}
bool update()
{
int tmp[3] = {0, 0, 0};
FILE *fd;
if (!(fd = fopen(ACCELEROMETER_FILE_N900, "r"))) {
return false;
}
if (fscanf(fd, "%i %i %i", tmp, tmp+1, tmp+2) != 3) {
return false;
}
if (fclose(fd) == EOF) {
return false;
}
x = tmp[0];
y = tmp[1];
z = tmp[2];
//How to change this?
writeln('x=%i',x);
return true;
}
int getX() { return x; }
int getY() { return y; }
int getZ() { return z; }
};
int main(int argc, char *argv[])
{
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
The object name of label is label_2. Which code do I have to write to change Label2 text to accelerometer X value? Or what else do I have to write? Or do you have any examples of codes which show on smartphone view acceleration values with only one *.cpp file without any addidtional *.h files?
Re: How to write code in C language instead of QML?
Correct me if I'm wrong but N8 is Symbian based, so the path to the accelerometer is incorrect. Why don't you use QSensor from QtMobility instead?
Re: How to write code in C language instead of QML?
This page doesn't work. But on site: http://doc.qt.nokia.com/qtmobility-1...lerometer.html there is QML language, which I don't know. I would like to program it in C language. What do you suggest to do in this situation? I found on the internet codes with using accelerometers, but they were with some *.h files and I had lost in this. I would like to write a program with only one main.cpp file and no any header files and 6 Labels in main window: 3 of them- Accelerometer X/Y/Z, and other 3 of them would write actual acceleration. In future I would like to add speed in additional 3 labels.
Re: How to write code in C language instead of QML?
Quote:
Originally Posted by
tom634
This page doesn't work.
Yes, it doesn't work -- blame autolinking of the site :D
Here you go: http://doc.qt.nokia.com/qtmobility-1.2/sensors-api.html
QAccelerometer is a C++ class, not a QML element. The latter is called Accelerometer. And please don't mix C and C++, those are two different languages.
Re: How to write code in C language instead of QML?
Code:
#include "mainwindow.h"
#include <QtGui/QApplication>
#include <QAccelerometer>
int main(int argc, char *argv[])
{
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
// On the heap (deleted when this object is deleted)
QAccelerometer *sensor = new QAccelerometer(this);
// On the stack (deleted when the current scope ends)
QOrientationSensor orient_sensor;
// start the sensor
QSensor sensor("QAccelerometer");
sensor.start();
// later
QSensorReading *reading = sensor.reading();
qreal x = reading->property("x").value<qreal>();
qreal y = reading->value(1).value<qreal>();
QAccelerometer sensor;
sensor.setProperty("maximumReadingCount", 100);
sensor.setProperty("processAllReadings", true);
return app.exec();
}
With using this code which I written basing on the codes from your site I still receive compilation errors. For example
C:\...\main.cpp:5: error: QAccelerometer: No such file or directory
Or
C:\...\main.cpp:16: error: 'QAccelerometer' was not declared in this scope
or
C:\...\main.cpp:16: error: expected type-specifier before 'QAccelerometer'
How to repair this?
Re: How to write code in C language instead of QML?
You probably didn't enable mobility/sensor support for your project. Or you simply don't have Qt Mobility installed.
Re: How to write code in C language instead of QML?
Is it enough to enable sensor support in my project if I select Qt Widget Project->Mobile Qt Application? What else do I have to do to enable sensor support?
Is there anywhere easy instalation of Qt Mobility? When I select configure file in Qt Mobility open source *.rar file it shows cmd window for a moment and it doesn't install Qt Mobility. And "nmake" and "nmake install" commands don't work.
Re: How to write code in C language instead of QML?
Quote:
Originally Posted by
tom634
Is it enough to enable sensor support in my project if I select Qt Widget Project->Mobile Qt Application?
No.
Quote:
What else do I have to do to enable sensor support?
You need to read QtMobility docs. I suspect giving you a direct answer would not help you with anything as you'd come back again in 10 minutes with a next problem related to not reading the manual prior to using some solution.
Quote:
Is there anywhere easy instalation of Qt Mobility? When I select configure file in Qt Mobility open source *.rar file it shows cmd window for a moment and it doesn't install Qt Mobility. And "nmake" and "nmake install" commands don't work.
I've never heard of any rar file containing Qt Mobility so I can't help.
Re: How to write code in C language instead of QML?
Are you sure you are using the right package? As Qt Mobility is available as part of the Qt SDK.