1
votes

So this may seem like a strange setup. I have a C++ object that inherits from QObject called "MasterGuiLogic" for simplicity. It is created with a pointer to another object called "MainEventBroker" which as you might guess handles all of my applications events. The MasterGuiLogic object is registered with qml as a context property so that it's properties can be used anywhere in my qml. So main.cpp looks like this:

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    MasterEventBroker *MainEventBroker = new MasterEventBroker();
    MasterGuiLogic *MainGuiLogic = new MasterGuiLogic(*MainEventBroker);

    qmlRegisterUncreatableType<MasterGuiLogic>("GrblCom", 1, 0, "MasterGuiLogic", "");
    qmlRegisterUncreatableType<GuiLogic_SerialCom>("GrblCom", 1, 0, "GuiLogic_SerialCom", "");

    QQmlApplicationEngine engine;
    QQmlContext* context = engine.rootContext();

    context->setContextProperty("MasterGuiLogic", &(*MainGuiLogic));

    engine.load(QUrl(QLatin1String("qrc:/QmlGui/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

MasterGuiLogic creates an instance of another class called SerialCom, which is set as a Q_PROPERTY so that it's properties and public slots can be reached in qml through the MasterGuiLogic property.

MasterGuiLogic.h:

class MasterGuiLogic : public QObject
{
    Q_OBJECT
    Q_PROPERTY(GuiLogic_SerialCom* serialCom READ serialCom CONSTANT)
public:
    MasterEventBroker *eventBroker;

    explicit MasterGuiLogic(MasterEventBroker &ev, QObject *parent = nullptr);


    GuiLogic_SerialCom* serialCom() const {
        return Gui_SerialCom;
    }

private:

    GuiLogic_SerialCom *Gui_SerialCom;

MasterGuiLogic.cpp:

MasterGuiLogic::MasterGuiLogic(MasterEventBroker &ev, QObject *parent) : QObject(parent)
{
    this->eventBroker = &ev;
    this->Gui_SerialCom = new GuiLogic_SerialCom(this);
}

SerialCom.h:

//Forward Declare our parent
class MasterGuiLogic;

class GuiLogic_SerialCom : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QStringList portNames READ portNames NOTIFY portNamesChanged)
    Q_PROPERTY(bool connectedToPort READ connectedToPort NOTIFY connectedToPortChanged)
public:

    MasterGuiLogic *parent;
    explicit GuiLogic_SerialCom(MasterGuiLogic *parent = nullptr);

    std::map<QString, QSerialPortInfo> portsMap;


    QStringList portNames() {
        return _portNames;
    }

    bool connectedToPort() {
        return _connectedToPort;
    }

private:

    QStringList _portNames;
    bool _connectedToPort = false;


signals:

    void portNamesChanged(const QStringList &);
    void connectedToPortChanged(const bool &);

public slots:

    void connectToPort(const QString portName);
    void disconnectFromPort(const QString portName);

};

SerialCom.cpp:

GuiLogic_SerialCom::GuiLogic_SerialCom(MasterGuiLogic *parent) : QObject(qobject_cast<QObject *>(parent))
{
    this->parent = parent;


    QList<QSerialPortInfo> allPorts = QSerialPortInfo::availablePorts();

    for (int i = 0; i < allPorts.size(); ++i) {
        this->_portNames.append(allPorts.at(i).portName());
        this->portsMap[allPorts.at(i).portName()] = allPorts.at(i);
    }

    emit portNamesChanged(_portNames);

}

void GuiLogic_SerialCom::connectToPort(const QString portName) {
    //TODO: Connect To Port Logic Here;

    //Set Connected
    this->_connectedToPort = true;
    emit connectedToPortChanged(this->_connectedToPort);

    qDebug() << portName;
}

void GuiLogic_SerialCom::disconnectFromPort(const QString portName) {
    //TODO: DisConnect To Port Logic Here;

    //Set DisConnected
    this->_connectedToPort = false;
    emit connectedToPortChanged(this->_connectedToPort);

    qDebug() << portName;
}

So from qml it's pretty easy to read any of these properties and even send signals from qml to c++

For example, this works just fine:

connectCom.onClicked: {
        if (MasterGuiLogic.serialCom.connectedToPort === false) {
            MasterGuiLogic.serialCom.connectToPort(comPort.currentText);
        } else {
            MasterGuiLogic.serialCom.disconnectFromPort(comPort.currentText);
        }
    }

The problem is, I can't seem to find a way to connect to signals that are emitted from SerialCom. I thought I would be able to do something like this:

Connections: {
        target: MasterGuiLogic.serialCom;
        onConnectedToPortChanged: {
            if (MasterGuiLogic.serialCom.connectedToPort === false) {
                connectCom.text = "Disconnect";
                comPort.enabled = false;
            } else {
                connectCom.text = "Connect";
                comPort.enabled = true;
            }
        }
    }

This should listen to the boolean property on SerialCom to change, but I get the following error:

QQmlApplicationEngine failed to load component
qrc:/QmlGui/main.qml:21 Type Page1 unavailable
qrc:/QmlGui/Page1.qml:49 Invalid attached object assignment

This just means that I can't "connect" using the target line above. Is there any other way I can connect to signals from a Q_PROPERTY of type QObject inside a ContextProperty?

2
what is Page1? I see no errors about wrong connection. Why do you think the error output relates to the Connections?folibis

2 Answers

2
votes

Ok, I found the problem... The provided answers, while indeed helpful for other reasons, were not the correct solution. After going over the code by @Xplatforms, I couldn't figure out what the difference was between what I was doing and what he did.... until I saw this in my own code:

Connections: {
        target: MasterGuiLogic.serialCom;
        onConnectedToPortChanged: {
            ...
        }
    }

There isn't supposed to be a colon(:) there...

Connections {
        target: MasterGuiLogic.serialCom;
        onConnectedToPortChanged: {
            ...
        }
    }

Never try programming while sleepy...lol

1
votes

First of all what should &(*MainGuiLogic) mean? You are dereferencing and referencing again the MainGuiLogic? Why?

context->setContextProperty("MasterGuiLogic", MainGuiLogic); will be enought.

But registering MasterGuiLogic as Type and adding the Object named MasterGuiLogic can overide themself in QML world.

Set it like context->setContextProperty("MyGuiLogic", MainGuiLogic); to eleminate this behavior.

Also don't pass references between C++ and QML worlds like:

void connectedToPortChanged(**const bool &**);.

Just use atomic type and values (const bool); and give it a name, to be able to use it as named value in QML:

void connectedToPortChanged(bool connected)

Here is an example with the structure like yours, which works. Just click in window and look in output console.

test.h:

#ifndef TEST_H
#define TEST_H

#include <QObject>

class GuiLogic_SerialCom : public QObject
{
    Q_OBJECT
public:
    GuiLogic_SerialCom(){}

signals:
    void connectedToPortChanged(bool connected);

public slots:
    void connectToPort(const QString & portName);
};

class MasterGuiLogic : public QObject
{
    Q_OBJECT
public:
    MasterGuiLogic();

    Q_PROPERTY(GuiLogic_SerialCom * serialCom READ serialCom CONSTANT)
    GuiLogic_SerialCom* serialCom() const {return test;}

    Q_INVOKABLE void generate_signal();

private:
    GuiLogic_SerialCom * test;
};

#endif // TEST_H

test.cpp:

#include "test.h"
#include <QDebug>

MasterGuiLogic::MasterGuiLogic()
{
    this->test = new GuiLogic_SerialCom();
}

void MasterGuiLogic::generate_signal()
{
    qDebug() << __FUNCTION__ << "Calling serialcom to gen signal";
    this->test->connectToPort("88");
}


void GuiLogic_SerialCom::connectToPort(const QString &portName)
{
    qDebug() << __FUNCTION__ << "got signal" << portName;
    emit this->connectedToPortChanged(true);
}

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "test.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    qmlRegisterUncreatableType<MasterGuiLogic>("GrblCom", 1, 0, "MasterGuiLogic", "");
    qmlRegisterUncreatableType<GuiLogic_SerialCom>("GrblCom", 1, 0, "GuiLogic_SerialCom", "");

    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty(QStringLiteral("Test"), new MasterGuiLogic());

    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

import GrblCom 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            Test.generate_signal();
        }
    }

    Connections
    {
        target: Test.serialCom
        onConnectedToPortChanged:
        {
            console.log("Got signal from SerialCom in QML. passed bool value is: " + connected);
        }
    }
}