You are currently browsing the archives for 14 November 2016.
Displaying 1 - 4 of 4 entries.

Integrating C++ with QML

  • Posted on November 14, 2016 at 9:15 pm

Introduction

Qt Quick’s QML language makes it easy to do many things, especially fancy animated user interfaces. However, some things either can’t be done or are not suitable for implementing in QML, such as:

  1. Getting access to functionality outside of the QML/JavaScript environment.
  2. Implementing performance critical functions where native code is desired for efficiency.
  3. Large and/or complex non-declarative code that would be tedious to implement in JavaScript.

As we’ll see, Qt makes it quite easy to expose C++ code to QML. In this blog post I will show an example of doing this with a small but functional application.

The example is written for Qt 5 and uses the Qt Quick Components so you will need at least Qt version 5.1.0 to run it.

Overview

To expose a C++ type having properties, methods, signals, and/or slots to the QML environment, the basic steps are:

  1. Define a new class derived from QObject.
  2. Put the Q_OBJECT macro in the class declaration to support signals and slots and other services of the Qt meta-object system.
  3. Declare any properties using the Q_PROPERTY macro.
  4. Call qmlRegisterType() in your C++ main program to register the type with the Qt Quick engine.

For all the details I refer you to the Qt documentation section Exposing Attributes of C++ Types to QML and the Writing QML Extensions with C++ tutorial.

Ssh Key Generator

For our code example, we want a small application that will generate ssh public/private key pairs using a GUI. It will present the user with controls for the appropriate options and then run the program ssh-keygen to generate the key pair.

I implemented the user interface using the new Qt Quick Controls since it was intended as a desktop application with a desktop look and feel. I initially developed the UX entirely by running the qmlscene program directly on the QML source.

The UI prompts the user for the key type, the file name of the private key to generate and an optional pass phrase, which needs to be confirmed.

The C++ Class

Now that have the UI, we will want to implement the back end functionality. You can’t invoke an external program directly from QML so we have to write it in C++ (which is the whole point of this example application).

First, we define a class that encapsulates the key generation functionality. It will be exposed as a new class KeyGenerator in QML. This is done in the header file KeyGenerator.h below.

#ifndef KEYGENERATOR_H
#define KEYGENERATOR_H

#include <QObject>
#include <QString>
#include <QStringList>

// Simple QML object to generate SSH key pairs by calling ssh-keygen.

class KeyGenerator : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString type READ type WRITE setType NOTIFY typeChanged)
    Q_PROPERTY(QStringList types READ types NOTIFY typesChanged)
    Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
    Q_PROPERTY(QString passphrase READ filename WRITE setPassphrase NOTIFY passphraseChanged)

public:
    KeyGenerator();
    ~KeyGenerator();

    QString type();
    void setType(const QString &t);

    QStringList types();

    QString filename();
    void setFilename(const QString &f);

    QString passphrase();
    void setPassphrase(const QString &p);

public slots:
    void generateKey();

signals:
    void typeChanged();
    void typesChanged();
    void filenameChanged();
    void passphraseChanged();
    void keyGenerated(bool success);

private:
    QString _type;
    QString _filename;
    QString _passphrase;
    QStringList _types;
};
#endif

Next, we need to derive our class from QObject. We declare any properties that we want and the associated methods. Notify methods become signals. In our case, we want to have properties for the selected key type, the list of all valid ssh key types, file name and pass phrase. I arbitrarily made the key type a string. It could have been an enumerated type but it would have made the example more complicated.

Incidentally, a new feature of the Q_PROPERTY macro in Qt 5.1.0 is the MEMBER argument. It allows specifying a class member variable that will be bound to a property without the need to implement the setter or getter functions. That feature was not used here.

We declare methods for the setters and getters and for signals. We also declare one slot called generateKey(). These will all be available to QML. If we wanted to export a regular method to QML, we could mark it with Q_INVOCABLE. In this case I decided to make generateKey() a slot since it might be useful in the future but it could have just as easily been an invocable method.

Finally, we declare any private member variables we will need.

C++ Implementation

Now let’s look at the implementation in KeyGenerator.cpp. Here is the source code:

#include <QFile>
#include <QProcess>
#include "KeyGenerator.h"

KeyGenerator::KeyGenerator()
    : _type("rsa"), _types{"dsa", "ecdsa", "rsa", "rsa1"}
{
}

KeyGenerator::~KeyGenerator()
{
}

QString KeyGenerator::type()
{
    return _type;
}

void KeyGenerator::setType(const QString &t)
{
    // Check for valid type.
    if (!_types.contains(t))
        return;

    if (t != _type) {
        _type = t;
        emit typeChanged();
    }
}

QStringList KeyGenerator::types()
{
    return _types;
}

QString KeyGenerator::filename()
{
    return _filename;
}

void KeyGenerator::setFilename(const QString &f)
{
    if (f != _filename) {
        _filename = f;
        emit filenameChanged();
    }
}

QString KeyGenerator::passphrase()
{
    return _passphrase;
}

void KeyGenerator::setPassphrase(const QString &p)
{
    if (p != _passphrase) {
        _passphrase = p;
        emit passphraseChanged();
    }
}

void KeyGenerator::generateKey()
{
    // Sanity check on arguments
    if (_type.isEmpty() or _filename.isEmpty() or
        (_passphrase.length() > 0 and _passphrase.length() < 5)) {
        emit keyGenerated(false);
        return;
    }

    // Remove key file if it already exists
    if (QFile::exists(_filename)) {
        QFile::remove(_filename);
    }

    // Execute ssh-keygen -t type -N passphrase -f keyfileq
    QProcess *proc = new QProcess;
    QString prog = "ssh-keygen";
    QStringList args{"-t", _type, "-N", _passphrase, "-f", _filename};
    proc->start(prog, args);
    proc->waitForFinished();
    emit keyGenerated(proc->exitCode() == 0);
    delete proc;
}

The constructor initializes some of the member variables. For fun, I used the new initializer list feature of C++11 to initialize the _types member variable which is of type QStringList. The destructor does nothing, at least for now, but is there for completeness and future expansion.

Getter functions like type() simply return the appropriate private member variable. Setters set the appropriate variables, taking care to check that the new value is different from the old one and if so, emitting the appropriate signal. As always, please note that signals are created by the Meta Object Compiler and do not need to be implemented, only emitted at the appropriate times.

The only non-trivial method is the slot generateKey(). It does some checking of arguments and then creates a QProcess to run the external ssh-keygen program. For simplicity and because it typically executes quickly, I do this synchronously and block on it to complete. When done, we emit a signal that has a boolean argument that indicates the key was generated and whether it succeeded or not.

QML Code

Now let’s look at the QML code in main.qml:

// SSH key generator UI

import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import QtQuick.Dialogs 1.0
import com.ics.demo 1.0

ApplicationWindow {
    title: qsTr("SSH Key Generator")

    statusBar: StatusBar {
    RowLayout {
        Label {
            id: status
            }
        }
    }

    width: 369
    height: 166

    ColumnLayout {
        x: 10
        y: 10

        // Key type
        RowLayout {
            Label {
                text: qsTr("Key type:")
            }
            ComboBox {
                id: combobox
                Layout.fillWidth: true
                model: keygen.types
                currentIndex: 2
            }
        }

        // Filename
        RowLayout {
            Label {
                text: qsTr("Filename:")
            }
            TextField {
                id: filename
                implicitWidth: 200
                onTextChanged: updateStatusBar()
            }
            Button {
                text: qsTr("&Browse...")
                onClicked: filedialog.visible = true
            }
        }

        // Passphrase
        RowLayout {
            Label {
                text: qsTr("Pass phrase:")
            }
            TextField {
                id: passphrase
                Layout.fillWidth: true
                echoMode: TextInput.Password
                onTextChanged: updateStatusBar()
            }

        }

        // Confirm Passphrase
        RowLayout {
            Label {
                text: qsTr("Confirm pass phrase:")
            }
            TextField {
                id: confirm
                Layout.fillWidth: true
                echoMode: TextInput.Password
                onTextChanged: updateStatusBar()
            }
        }

        // Buttons: Generate, Quit
        RowLayout {
            Button {
                id: generate
                text: qsTr("&Generate")
                onClicked: keygen.generateKey()
            }
            Button {
                text: qsTr("&Quit")
                onClicked: Qt.quit()
            }
        }

    }

    FileDialog {
        id: filedialog
        title: qsTr("Select a file")
        selectMultiple: false
        selectFolder: false
        nameFilters: 
        selectedNameFilter: "All files (*)"
        onAccepted: {
            filename.text = fileUrl.toString().replace("file://", "")
        }
    }

    KeyGenerator {
        id: keygen
        filename: filename.text
        passphrase: passphrase.text
        type: combobox.currentText
        onKeyGenerated: {
            if (success) {
                status.text = qsTr('<font color="green">Key generation succeeded.</font>')
            } else {
                status.text = qsTr('<font color="red">Key generation failed</font>')
            }
        }
    }

    function updateStatusBar() {
        if (passphrase.text != confirm.text) {
            status.text = qsTr('<font color="red">Pass phrase does not match.</font>')
            generate.enabled = false
        } else if (passphrase.text.length > 0 && passphrase.text.length < 5) {
            status.text = qsTr('<font color="red">Pass phrase too short.</font>')
            generate.enabled = false
        } else if (filename.text == "") {
            status.text = qsTr('<font color="red">Enter a filename.</font>')
            generate.enabled = false
        } else {
            status.text = ""
            generate.enabled = true
        }
    }

    Component.onCompleted: updateStatusBar()
}

The preceding code is a little long, however, much of the work is laying out the GUI components. The code should be straightforward to follow.

Note that we import com.ics.demo version 1.0. We’ll see where this module name comes from shortly. This makes a new QML type KeyGeneratoravailable and so we declare one. We have access to it’s C++ properties as QML properties, can call it’s methods and act on signals like we do withonKeyGenerated.

A more complete program should probably do a little more error checking and report meaningful error messages if key generation fails (we could easily add a new method or property for this). The UI layout could also be improved to make it properly resizable.

Our main program is essentially a wrapper like qmlscene. All we need to do to register our type with the QML engine is to call:

    qmlRegisterType<KeyGenerator>("com.ics.demo", 1, 0, "KeyGenerator");

This makes the C++ type KeyGenerator available as the QML type KeyGenerator in the module com.ics.demo version 1.0 when it is imported.

Typically, to run QML code from an executable, in the main program you would create a QGuiApplication and a QQuickView. Currently, to use the Qt Quick Components there is some additional work needed if the top level element is an ApplicationWindow or Window. You can look at the source code to see how I implemented this. I basically stripped down the code from qmlscene to the minimum of what was needed for this example.

Here is the full listing for the main program, main.cpp:

#include <QApplication>
#include <QObject>
#include <QQmlComponent>
#include <QQmlEngine>
#include <QQuickWindow>
#include <QSurfaceFormat>
#include "KeyGenerator.h"

// Main wrapper program.
// Special handling is needed when using Qt Quick Controls for the top window.
// The code here is based on what qmlscene does.

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

    // Register our component type with QML.
    qmlRegisterType<KeyGenerator>("com.ics.demo", 1, 0, "KeyGenerator");

    int rc = 0;

    QQmlEngine engine;
    QQmlComponent *component = new QQmlComponent(&engine);

    QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));

    component->loadUrl(QUrl("main.qml"));

    if (!component->isReady() ) {
        qWarning("%s", qPrintable(component->errorString()));
        return -1;
    }

    QObject *topLevel = component->create();
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);

    QSurfaceFormat surfaceFormat = window->requestedFormat();
    window->setFormat(surfaceFormat);
    window->show();

    rc = app.exec();

    delete component;
    return rc;
}

In case it is not obvious, when using a module written in C++ with QML you cannot use the qmlscene program to execute your QML code because the C++ code for the module will not be linked in. If you try to do this you will get an error message that the module is not installed.

WhatsApp Luncurkan Fitur Baru di Windows Phone

  • Posted on November 14, 2016 at 5:05 pm

Jakarta – WhatsApp is one of the instant messaging service that can be used by many types of cell phone operating system. Not surprisingly, WhatsApp has 250 million active users. According to the Wall Street Journal website, this amount is equivalent to the number of users on Twitter, Facebook Messenger, and Skype.
What’s more, the instant messaging application also does not include ads in their services. Users were not bothered by ads that sometimes appear like the other instant messaging services.
Now, WhatsApp update the application with new features for Windows Phone users. Some of the new features that include
1. Make voice calls;
2. Background API (Application Programming Interface) which can play music;
3. New Smiley;
4. Notifications are updated to the user;
5. Quick summary (if the application is open and the user clicks the notification, the application will open fast like on Android);
6. The user will be asked whether you want to save the conversation history, users can arrange it through the settings menu, and
7. Images are displayed larger.
Updates on Windows Phone devices can be downloaded in Windows Phone Store.

MontaVista Software Extends Support for ARM® Architecture Targeting Telecom and Networking Markets

  • Posted on November 14, 2016 at 1:32 pm

SAN JOSE, Calif., July 2, 2013 /PRNewswire/ — MontaVista® Software, Inc., the leader in embedded Linux®commercialization, today announced Carrier Grade Edition® (CGE) support for the Carrier Grade Linux 5.0 profile for ARM architecture.  This milestone marks the first CGL registered product to support the ARM architecture. The tidal wave of smart phone and tablet usage has created a situation where mobile broadband demand is outpacing infrastructure capability. Carriers are racing to expand capacity while reducing the power required to run the mobile broadband telecommunications infrastructure.  For almost a decade, Telecom OEM and carriers have defined their Linux requirements using the Carrier Grade Linux specification.  MontaVista has bridgecd the gap between next generation silicon on ARM and Telecom Linux requirements.

“To support ARM-based SoC designs for carrier and cloud equipment, we recognize the importance of carrier grade software platforms to be in lock step with those silicon implementations, as this will accelerate time-to-deployment for highly reliable, available and secure next-generation equipment,” said Bob Monkman, manager, Enterprise Networking Segment for ARM. “MontaVista pioneered the Carrier Grade Linux movement, and it continues to be a leading innovator for this software platform that remains the crucial benchmark for network equipment and data centers alike. This milestone is another proof point that the necessary software ecosystem is in place for ARM-based systems to deploy into the global communications network.”

MontaVista’s Carrier Grade Edition is designed for high reliability infrastructure markets. CGE is the standard foundation of a Linux based platform, certified to meet performance requirements, high availability, serviceability, hardening, and real-time response.  The CGE multi-architecture platform allows customers to cross compile across all major architectures knowing they have met all CGL, LSB, and IPv6 requirements.  Only MontaVista provides a Carrier Grade Linux cross-architecture platform that allows telecom & network equipment manufacturers to cross compile from other architectures to ARM for their next-generation devices.

“As the provider of the world’s most widely-deployed Carrier Grade Linux, MontaVista is committed to supporting the ARM ecosystem with certified and high-performance operating systems.” said Patrick MacCartee, Director of Marketing for MontaVista Software. ”

MontaVista is bridging the gap between IT and Telco Linux operations systems by providing leadership in the Linaro Network Group (LNG), where it sits on the steering committee, as well as being part of the Carrier Grade Linux community. Our unique experience enables us to provide the best-in-class platform for ARM in cloud and carrier infrastructure applications.  MontaVista is supporting the ARM architecture for a range of applications in the telecom supply chain.  Work is underway to provide KVM-based virtualization to enable cloud-based solutions for mobile core and data plane on the ARM architecture.

“MontaVista has led the way in providing Carrier Grade Linux (CGL) support since the first Requirements Definition document in 2002,” said Mark Orvek, Linaro VP of Engineering. “We’re pleased to see MontaVista Linux Carrier Grade Edition listed by the Linux Foundation as the first distribution to implement the CGL specification on the ARM platform and we’re very happy to be working together with MontaVista and the other industry-leading members of the Linaro Networking Group to develop the future of Linux on ARM in this space.”

MontaVista is committed to compliance with the major industry standards and maintains its position of being the only Linux distribution in the world to comply with the three key requirements issued by the industry’s major standards bodies: CGL, Linux Standard Base (LSB), and IPv6. MontaVista’s Carrier Grade Edition is also the only embedded Linux to be Oracle-certified. This certification demonstrates MontaVista’s ongoing and continued commitment to CGE interoperability with industry software and hardware, and meets the rigorous demands of current and future multi-core network infrastructures. MontaVista has made available copies of the CGL5 registration documents on its website at http://www.mvista.com/products/cge/cgl/cgl.php.

“We applaud MontaVista’s continued leadership with Carrier Grade Linux for the carrier infrastructure market,” said Amanda McPherson, vice president of marketing and developer services Linux Foundation. “MontaVista’s support of the CGL specification for the ARM architecture will be key to enabling a smooth migration to this important SoC architecture that supports millions of devices worldwide.”

“Carrier grade” is a term for software and hardware products that support public telecommunications and data communications networks. Carrier grade products require extremely high degrees of reliability, scalability, and performance to provide an uninterrupted flow of the enormous volume of high-bandwidth data and voice needed for today’s multimedia communications. MontaVista Linux Carrier Grade Edition is the most widely deployed carrier grade Linux solution in the world, and is used by leading network equipment providers (NEPs) including Alcatel-Lucent, Motorola, NEC, and other leading suppliers.

About MontaVista Software
MontaVista Software, LLC, a wholly owned subsidiary of Cavium, Inc. (CAVM), is a leader in embedded Linux commercialization. For over 10 years, MontaVista has been helping embedded developers get the most out of open source by adding commercial quality, integration, hardware enablement, expert support, and the resources of the MontaVista development community. Because MontaVista customers enjoy faster time to market, more competitive device functionality, and lower total cost, more devices have been deployed with MontaVista than with any other Linux.

Thanko Quad-core Android SmartTV 2, PC Portable LCD TV to Change Android Devices Being Great

  • Posted on November 14, 2016 at 5:23 am

Want to feel the sensation of the Android experience to a jolly? If so, the latest portable PCs known as Thanko Quad-core Android SmartTV 2 this might be an alternative solution.

With features and capabilities that it has, you can use Android SmartTV Thanko Quad-core 2 is to convert non-Adroid devices such as LCD TV into an Android device.

PC portable measuring 122x175x121mm and weighs 250g is complemented by a 1.6GHz Quad Core CPU, operating system Android 4.2 Jelly Bean, and a built-in 2GB memory. And thanks to the existence of two existing USB terminal, you can use it with a keyboard, mouse, portable HDD and so on.

While about prices in Japan itself, Thanko Quad-core Android SmartTV 2 priced around ¥ 14,800 or the equivalent of 1.5 million dollars per unit.