Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using C++ with QML
QtWS25 Last Chance

Using C++ with QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bobweaver
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • P Offline
        P Offline
        portoist
        wrote on last edited by
        #3

        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 .

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bobweaver
          wrote on last edited by
          #4

          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 .

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved