1
votes

Okay I'm resetting this whole post because I guess I didn't have enough " Minimal, Complete, and Verifiable example " which really is the entirety of my question, because I am just so LOST on slots and signals.. so here's 2nd attempt, I will leave out flower.cpp, but know it has a function in there

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QtQuick>
#include <QNetworkAccessManager>
#include <iostream>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QNetworkReply>
#include <QObject>
#include "flower.h"



void Flower::onClicked(){
//code i've been trying to test all day
}

flower.h (my header for the class flower (the function))

#ifndef FLOWER_H
#define FLOWER_H

#include <QObject>

class Flower
{
private slots:
void onClicked();

};
#endif // FLOWER_H

main.cpp (this is where my app QML is started from, and I'm trying to setup the connection of signal and slot there)

QQuickView home;
home.setSource(QUrl::fromLocalFile("main.qml"));
home.show();
QObject *homePa = home.rootObject();
QObject *buttF = homePa->findChild<QObject*>("buttFObject");
QObject::connect(buttF, SIGNAL(qmlClick()), buttF,
                 SLOT(Flower.onClicked()));

this is the navmenu with the mousearea that I want to have the onClicked: command attached

Rectangle {
    signal qmlClick();

    id: navMenu
    color: "#00000000"
    radius: 0
    anchors.fill: parent
    z: 3
    visible: false
    border.width: 0
    transformOrigin: Item.Center
               MouseArea {
                id: buttFArea
                objectName: buttFObject
                anchors.fill: parent
                onClicked: navMenu.qmlClick()
            }
           }

When I try to run right now I receive this error "W libAHDP.so: QObject::connect: Cannot connect (null)::qmlClick() to (null)::Flower.onClicked()"

Apologies for my first post being very misleading and mixed up I hope this is more clear on what my issue is

1
Please provide a minimal reproducible example - right now your question has far too much code in it that isn't relevant to the actual problem, making it much harder for anyone to help.BoBTFish

1 Answers

0
votes

Only QObjects can have slots so Flower must inherit from QObject. On the other hand you are using an approach that always brings problems that is to try to obtain a QML element from C++, instead you must export the C++ element to QML using setContextProperty():

flower.h

#ifndef FLOWER_H
#define FLOWER_H

#include <QObject>

class Flower : public QObject
{
    Q_OBJECT
public:
    explicit Flower(QObject *parent = nullptr);
    Q_SLOT void onClicked();
};

#endif // FLOWER_H

flower.cpp

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

Flower::Flower(QObject *parent) : QObject(parent)
{}
void Flower::onClicked()
{
    qDebug()<< __PRETTY_FUNCTION__;
}

main.cpp

#include <QGuiApplication>
#include <QQuickView>
#include <QQmlContext>
#include "flower.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    Flower flower;
    QQuickView view;
    view.rootContext()->setContextProperty("flower", &flower);
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    view.show();
    return app.exec();
}

main.qml

import QtQuick 2.9

Rectangle {
    color: "#00000000"
    anchors.fill: parent
    transformOrigin: Item.Center
    MouseArea {
        id: buttFArea
        anchors.fill: parent
        onClicked: flower.onClicked()
    }
}

For more information I recommend reading Best Practices for QML and Qt Quick