Reputation: 31
I want to send clearPinChanged(const QString& pin) signal from ClearPin class when party->startParty(); invoked. now it does not work for me. may i send the signal without changing the structure of code?
the below code is one of Qt example which I just added ClearPin class to it.
generally how can I send signal to non main component like clearpin.qml which is created inside example.qml component?
birthdayparty.h
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H
#include <QObject>
#include <QDate>
#include <qqml.h>
#include "person.h"
#include "Clearpin.h"
class BirthdayPartyAttached : public QObject
{
Q_OBJECT
Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp)
public:
BirthdayPartyAttached(QObject *object);
QDate rsvp() const;
void setRsvp(const QDate &);
private:
QDate m_rsvp;
};
class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(Person *host READ host WRITE setHost)
Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
Q_CLASSINFO("DefaultProperty", "guests")
public:
BirthdayParty(QObject *parent = 0);
Person *host() const;
void setHost(Person *);
QQmlListProperty<Person> guests();
int guestCount() const;
Person *guest(int) const;
static BirthdayPartyAttached *qmlAttachedProperties(QObject *);
void startParty();
signals:
void partyStarted(const QTime &ttime);
private:
Person *m_host;
QList<Person *> m_guests;
ClearPin clearpin;
};
QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES)
#endif // BIRTHDAYPARTY_H
birthdayparty.cpp
#include "birthdayparty.h"
BirthdayPartyAttached::BirthdayPartyAttached(QObject *object)
: QObject(object)
{
}
QDate BirthdayPartyAttached::rsvp() const
{
return m_rsvp;
}
void BirthdayPartyAttached::setRsvp(const QDate &d)
{
m_rsvp = d;
}
BirthdayParty::BirthdayParty(QObject *parent)
: QObject(parent), m_host(nullptr)
{
}
Person *BirthdayParty::host() const
{
return m_host;
}
void BirthdayParty::setHost(Person *c)
{
m_host = c;
}
QQmlListProperty<Person> BirthdayParty::guests()
{
return QQmlListProperty<Person>(this, m_guests);
}
int BirthdayParty::guestCount() const
{
return m_guests.count();
}
Person *BirthdayParty::guest(int index) const
{
return m_guests.at(index);
}
void BirthdayParty::startParty()
{
QTime ttime = QTime::currentTime();
clearpin.setClearPin("4");
emit partyStarted(ttime);//it works
}
BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object)
{
return new BirthdayPartyAttached(object);
}
Clearpin.h
#ifndef CLEARPIN_H
#define CLEARPIN_H
#include <QObject>
class ClearPin : public QObject
{
Q_OBJECT
// Q_PROPERTY(QString clearPin READ clearPin WRITE setClearPin)
public:
explicit ClearPin(QObject *parent = nullptr);
virtual ~ClearPin();
QString clearPin() const;
void setClearPin(const QString & pin);
void removeClearPing(int n);
void removeAll();
private:
QString m_clearPin;
signals:
void clearPinChanged(const QString& pin);
};
#endif // CLEARPIN_H
Clearpin.cpp
#include "Clearpin.h"
#include <QDebug>
ClearPin::ClearPin(QObject *parent) : QObject(parent)
{
m_clearPin="";
}
ClearPin::~ClearPin()
{
}
QString ClearPin::clearPin() const
{
return m_clearPin;
}
void ClearPin::setClearPin(const QString &pin)
{
qDebug()<<"setClearPin:"<<pin.toStdString().c_str();
m_clearPin=pin;
emit clearPinChanged(m_clearPin); //it does not work ???????????
}
void ClearPin::removeClearPing(int n)
{
m_clearPin.chop(n);
}
void ClearPin::removeAll()
{
m_clearPin.clear();
}
main.cpp
#include <QCoreApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include "birthdayparty.h"
#include "person.h"
#include "Clearpin.h"
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
qmlRegisterType<BirthdayPartyAttached>();
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
qmlRegisterType<ClearPin>("ClearPin", 1,0, "ClearPin");
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:example.qml"));
BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create());
party->startParty();
return 0;
}
example.qml
import People 1.0
import QtQuick 2.0 // For QColor
BirthdayParty {
property var component
property var object
onPartyStarted:{
console.log("This party started rockin' at " + ttime);
component= Qt.createComponent("clearpin.qml");
object=component.createObject(rootRectId);
}
}
clearpin.qml
import QtQuick 2.0
import ClearPin 1.0
Item {
ClearPin{
onClearPinChanged: {
console.log("pinnnn:",pin)
}
}
}
Upvotes: 0
Views: 523
Reputation: 8277
The problem is your BirthdayParty class emits the signal from it's instance of ClearPin, but in your QML code, you create a second instance of ClearPin and listen for its signal instead.
From looking at your definition of ClearPin, I'm not clear what it's purpose is. It looks like it's just a QString. If that's all you need, then you can just make the string a property of the BirthdayParty class, like this:
class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(QString clearPin READ clearPin WRITE setClearPin NOTIFY clearPinChanged)
public:
...
QString clearPin();
void setClearPin(QString pin)
signals:
void clearPinChanged();
}
In your QML, you should then be able to listen for its signal:
BirthdayParty {
onClearPinChanged: {
console.log("clearPin: " + clearPin);
}
}
Upvotes: 2