Category Archives: My

How to vibrate with Qt/QML on Android?

Short answer – pretty simple. Thanks to QtAndroidExtras

First we need to add to your .pro file:

android: QT += androidextras

 

then add simple class to your codebase. Header:

#ifndef VIBRATOR_H
#define VIBRATOR_H

#include <QObject>

#if defined(Q_OS_ANDROID)
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>
#endif
class Vibrator : public QObject
{
    Q_OBJECT
public:
    explicit Vibrator(QObject *parent = 0);
signals:
public slots:
    void vibrate(int milliseconds);
private:
#if defined(Q_OS_ANDROID)
    QAndroidJniObject vibratorService;
#endif
};

#endif // VIBRATOR_H

 

and the code:

#include "vibrator.h"
#include <QDebug>

Vibrator::Vibrator(QObject *parent) : QObject(parent)
{
#if defined(Q_OS_ANDROID)
    QAndroidJniObject vibroString = QAndroidJniObject::fromString("vibrator");
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
    vibratorService = appctx.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", vibroString.object<jstring>());
#endif
}

#if defined(Q_OS_ANDROID)

void Vibrator::vibrate(int milliseconds) {
    if (vibratorService.isValid()) {
        jlong ms = milliseconds;
        jboolean hasvibro = vibratorService.callMethod<jboolean>("hasVibrator", "()Z");
        vibratorService.callMethod<void>("vibrate", "(J)V", ms);
    } else {
        qDebug() << "No vibrator service available";
    }
}

#else
void Vibrator::vibrate(int milliseconds) {
    Q_UNUSED(milliseconds);
}

#endif

 

now you have to expose the class to QML:

#include "vibrator.h"
...
Vibrator vibrator;
engine.rootContext()->setContextProperty("Vibrator", &vibrator);

 

voila! its ready to use!

Vibrator.vibrate(500)

 

Enjoy

How to deal with dynamically created QQuickItem’s

Today I’d like to share some knowledge in form of small Qt C++/QML mixed project

The project is about chess game. Since its created for demonstration purposes, there is no battle mode, no AI and pieces moves checking missing some features such as en-passant etc

Code located here: ChessQML

Actually, inter-operate between C++ code and QML code in Qt framework pretty easy, however, there is few tips’n’tricks. For example: Make sure your objects’s pointer, created at C++ side and then passed to QML have to register its ownership to QML engine as C++ ownership:

QQmlEngine::setObjectOwnership(obj_ptr, QQmlEngine::CppOwnership);

This makes sense only for QObject’s derived classes

Please, leave your comments

 

Qt Quick Control’s TableView and C++ model using roles

Qt Quick Controls made big move forward in latest Qt releases. For example, in Qt 5.4 it takes native look on Android platform using styling option

Since it becomes more and more useful, its just waste of time to do not use it!

But its always there is some buts.

This time my stumbling-stone was TableView plus C++ data model

 

 

 

 

 

 

 

 

 

 

I’m not going to speechify a lot. Basically, standard scheme works, but my goal was to use roles as columns.In theory it was OK but every time I’ve met absence of data in my table. The only indicator that table works was increasing row counter

The problem was that roleNames() method of QAbstractItemModel class was not called. And the reason is that the method must be protected! For some reason this method in the Qt documentation as well as in tons of examples defined as public, and this is works in most cases but not for TableView!