← Back to team overview

ubuntu-phone team mailing list archive

connecting C++ to a QML button

 

Hello,

I'm trying to write my first QML/C++ app, so please be patient with me
:-)

The C++ main.cpp has:

...
int main(int argc, char *argv[]) {
  QGuiApplication app(argc, argv);
  QQuickView v;
  QUrl file = (QUrl) "myRoot/home/phablet/baresip-app/baresip.qml";

  v.setSource(file);
  v.show();

  MyClass myClass;

  QObject *item = v.rootObject();
  QObject *newButton = item->findChild<QObject*>("dialButton");
  QObject::connect(newButton, SIGNAL(qmlSignal(QString)),
                     &myClass, SLOT(cppSlot(QString)));

  return app.exec();
}

and the QML object is:

import QtQuick 2.4
import Ubuntu.Components 1.3


MainView {

    Rectangle {
        id: page
        width: 540; height: 960
        color: "lightgray"
        border.color: "grey"
        border.width: 5
        radius: 10
    
        Text {
            id: baresipTitle
            ...
        }
    
        ...

    Item {
        id: dialButton
        x: 10
        y: 110
        property alias cellColor: rectangle.color
        property alias cellText: buttonText.text

        signal qmlSignal(string msg)

        width: 160; height: 50

        Rectangle {
            id: rectangle
            border.color: "blue"
            color: "lightgrey"

            anchors.fill: parent

            Text {
                id: buttonText
                text: "Dial"
	        x: 8
                y: 24
                font.pointSize: 12
	    }    
        }

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onEntered: rectangle.border.color = "blue"
            onExited:  rectangle.border.color = "green"
    
            onClicked: button.qmlSignal("Hello from QML")
        }

        cellColor: buttonMouseArea.pressed ? Qt.darker(buttonColor, 1.5) : buttonColor
    }
    ...
}

The app starts fine but the C++ part does not find the "dialButton", it
says:

Loading module: 'libubuntu_application_api_touch_mirclient.so.3.0.0'
QObject::connect: Cannot connect (null)::qmlSignal(QString) to MyClass::cppSlot(QString)

Sure, that I do something wrong, but what?

Thanks for any help or pointer to help. The full project sources are
attached.

	matthias


-- 
Matthias Apitz, ✉ guru@xxxxxxxxxxx, ⌂ http://www.unixarea.de/  ☎ +49-176-38902045
"Die Verkaufsschlager des Buchmarkts geben Auskunft über den Zustand einer Gesellschaft bzw.
sind, was diese Zeiten angeht, Gradmesser fortschreitenden Schwachsinns. ..." (jW 19.05.2016)
QT += core

TARGET = baresip-app
CONFIG += console
CONFIG -= app_bundle

QT += widgets
QT += quick

TEMPLATE = app

HEADERS += myclass.h
SOURCES += main.cpp
SOURCES += myclass.cpp

import QtQuick 2.4
import Ubuntu.Components 1.3


MainView {

    Rectangle {
        id: page
        width: 540; height: 960
        color: "lightgray"
        border.color: "grey"
        border.width: 5
        radius: 10
    
        Text {
            id: baresipTitle
            text: "minimalistic SIP phone"
            y: 24
            anchors.horizontalCenter: page.horizontalCenter
            font.pointSize: 24; font.bold: true
        }
    
        TLineEditV2 {
            id: input1
            x: 10; y: 64 
            width: 520; height: 40
            focus: true
            text: "Command Input:  d echo@xxxxxxxxx "
        }
    
    Item {
        id: dialButton
        x: 10
        y: 110
        property alias cellColor: rectangle.color
        property alias cellText: buttonText.text

        signal qmlSignal(string msg)

        width: 160; height: 50

        Rectangle {
            id: rectangle
            border.color: "blue"
            color: "lightgrey"


            anchors.fill: parent

            Text {
                id: buttonText
                text: "Dial"
	        x: 8
                y: 24
                font.pointSize: 12
	    }    
        }

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onEntered: rectangle.border.color = "blue"
            onExited:  rectangle.border.color = "green"
    
            onClicked: button.qmlSignal("Hello from QML")
        }

        cellColor: buttonMouseArea.pressed ? Qt.darker(buttonColor, 1.5) : buttonColor
    }
    
        TextArea {
            id: outputBaresip
            x: 10; y: 160
            width: 520; height: 540
            text: "Output from baresip:\n\nbaresip is ready\nmapitz@xxxxxxxxx: {0/TCP/v4} 200 OK (ser (3.3.0-pre1 (i386/linux))) [1 binding]\nAll 1 useragent registered successfully! (2789 ms)\n\n\ndailing ...\n\n>                   echo@xxxxxxxxx\n call: connecting to 'sip:echo@xxxxxxxxx'..\n sip:echo@xxxxxxxxx: session closed: 480 Offline\n pulse: opening player (8000 Hz, 1 channels, device '')\n pulse: playback started\n pulse: stopping playback thread\n"
    	font.pointSize: 12
        }
    
    }

}
#include "myclass.h"

#include <QQuickView>
#include <QQuickItem>
#include <QQuickView>
#include <QCoreApplication>
#include <QtGui/QGuiApplication>


int main(int argc, char *argv[]) {
  QGuiApplication app(argc, argv);
  QQuickView v;
  QUrl file = (QUrl) "myRoot/home/phablet/baresip-app/baresip.qml";

  v.setSource(file);
  v.show();


  MyClass myClass;

  QObject *item = v.rootObject();
  QObject *newButton = item->findChild<QObject*>("dialButton");
  QObject::connect(newButton, SIGNAL(qmlSignal(QString)),
                     &myClass, SLOT(cppSlot(QString)));

  return app.exec();
}
#include "myclass.h"
#include <QObject>
#include <QDebug>

void MyClass::cppSlot(const QString *msg)
{
    // Q_OBJECT
    qDebug() << "Called the C++ slot with message:" << msg;
};

#include <QObject>

class MyClass : public QObject
{
    Q_OBJECT
public slots:
    void cppSlot(const QString *);
};

// TLineEditV2.qml

import QtQuick 2.4

FocusScope {
    width: 96; height: input.height + 8
    Rectangle {
        anchors.fill: parent
        color: "lightsteelblue"
        border.color: "gray"

    }

    property alias text: input.text
    property alias input: input

    TextInput {
        id: input
        anchors.fill: parent
        anchors.margins: 4
        focus: true
	font.bold: true
	font.pointSize: 12
    }
}


Follow ups