Using C++ with QML



  • Hello there and thanks for taking the time to look at this post. :)

    I want to use some c++ code that I have hacked away at in QML

    bonjourbrowser.h
    @
    #ifndef BONJOURBACKEND_h
    #define BONJOURBACKEND_h
    #include <QString>
    #include <QTcpSocket>
    #include "bonjourrecord.h"

    class BonjourServiceBrowser;
    class BonjourServiceResolver;
    class QHostInfo;
    class BonjourBackend : public QObject

    {
    Q_OBJECT
    public slots:
    void updateString(const QList<BonjourRecord> &list);
    public:
    BonjourBackend(QObject *parent = 0);

    private:
    quint16 blockSize;
    BonjourServiceBrowser *bonjourBrowser;
    BonjourServiceResolver *bonjourResolver;
    };
    #endif
    @

    bonjourbackend,cpp

    @
    #include <QtNetwork>
    #include "bonjourbackend.h"
    #include "bonjourservicebrowser.h"
    #include "bonjourserviceresolver.h"

    BonjourBackend::BonjourBackend(QObject *parent)
    : QObject(parent), bonjourResolver(0)
    {
    BonjourServiceBrowser *bonjourBrowser = new BonjourServiceBrowser(this);
    setenv("AVAHI_COMPAT_NOWARN","1",1);
    connect(bonjourBrowser, SIGNAL(currentBonjourRecordsChanged(const QList<BonjourRecord> &)),
    this, SLOT(updateString(const QList<BonjourRecord> &)));
    bonjourBrowser->browseForServiceType(QLatin1String("_mythbackend-master._tcp"));
    }
    void BonjourBackend::updateString(const QList<BonjourRecord> &list)
    {
    QString backend;
    foreach (BonjourRecord record, list) {
    backend.append(record.serviceName);
    qDebug() << backend;
    }
    }
    @

    main.cpp

    @
    #include "bonjourserviceresolver.h"
    #include "bonjourservicebrowser.h"
    #include "bonjourbackend.h"
    #include "bonjourrecord.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Mythbuntu-QML/main.qml"));
    viewer.showExpanded();
    viewer.show();

    BonjourBackend updateString;
    viewer.rootContext()->setContextProperty("backend",&updateString);
    return app.exec();
    }
    @

    The CPP runs great and returns the string that I am looking for but the QML on the other hand does not
    qml/Mythbuntu-QML/main.qml

    @
    import QtQuick 2.0
    import QtQuick.XmlListModel 2.0
    import QtQuick.Particles 2.0
    import QtQuick.LocalStorage 2.0
    import QtQuick.Window 2.0
    import Qt.labs.folderlistmodel 2.0
    import "common"
    import "common/models"
    import "common/dbUtil.js" as DataBase
    import "common/Util.js" as Util
    import "common/Remote.js" as Remote
    Rectangle{
    id: root
    ............
    .......................
    ..........................
    .................................

        Text{
            id: tname
            width: Screen.width / 20
            height:    Screen.height / 20
            color: Qt.darker("orange", 1.5)
            text:{
               var q =  backend
                return q.toString()
            } //drives + "\n" +   appThemePath+DataBase.theme()
            anchors{
                left:  themeRec.left
                top: themeRec.top
            }
        }
    

    ......................................
    ................................................
    .............................................................
    ....................................................................
    MouseArea{
    anchors.fill: mythImage
    onClicked: {
    var q = backend.toString()
    console.log(q)
    }
    }
    }
    .............
    ....................
    .
    ...........................
    }
    @

    Here is a the output from qDebug ()the one that I want in QML in the end
    @
    "Mythbackend on ubuntu"
    "Mythbackend on ubuntu"
    @

    and here is what I get with Mouse area and what not in QML
    @
    BonjourBackend(0xbfd46488)
    @

    Thanks again for taking the time to look this over and I look forward to solving this, Thanks


  • Moderators

    Your backend object does not contain "toString" method, as far as I can see. Add it as a slot or Q_INVOKABLE and it should work.



  • Calling toString on object in QML prints out type info about object. You should define property using Q_PROPERTY macro, or method using Q_INVOKABLE macro in your C++ backend and then use this in QML. Maybe you should take a look at this "doc":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-definetypes.html .



  • thanks all I was able to fix it with your and some IRC help. I am very new to the fourms and would like to say thanks you made me feel welcomed .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.