← Back to team overview

ubuntu-phone team mailing list archive

Re: connecting C++ to a QML button

 

More in general, always make sure you check the return value of findChild
to make sure it is actually finding something :)

On Fri, Jun 17, 2016 at 2:00 PM, Michael Terry <michael.terry@xxxxxxxxxxxxx>
wrote:

> Try adding:
> objectName: "dialButton"
>
> to dialButton?
>
> "id" is for inside-qml references.  "objectName" is for findChild and ilk.
>
> On Fri, Jun 17, 2016 at 8:58 AM, Matthias Apitz <guru@xxxxxxxxxxx> wrote:
>
>>
>> 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)
>>
>> --
>> Mailing list: https://launchpad.net/~ubuntu-phone
>> Post to     : ubuntu-phone@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~ubuntu-phone
>> More help   : https://help.launchpad.net/ListHelp
>>
>>
>
>
> --
> -mt
>
> --
> Mailing list: https://launchpad.net/~ubuntu-phone
> Post to     : ubuntu-phone@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~ubuntu-phone
> More help   : https://help.launchpad.net/ListHelp
>
>

References