Reputation: 303136
I have a C++ class Bar
that holds a reference to an instance of another class Foo
. When a method on Bar
is called, I want to emit a QML signal on the Foo
instance. I haven't coded in C++ in 20 years, and the syntax that QML uses to emit signals confuses me.
Foo.h
#include <QOpenGLFramebufferObject>
#include <QQuickFramebufferObject>
class Bar;
class Foo : public QQuickFramebufferObject {
Q_OBJECT
public:
Foo();
virtual ~Foo();
signals:
void huzzah();
private:
Bar &bar;
};
Foo.cpp
#include "Foo.h"
...
class Bar: public QObject {
Q_OBJECT
public:
Bar();
~Bar();
void SetItemAttached( QQuickItem &inItem );
public slots:
void BeforeRender();
private:
QQuickItem *foo;
};
void Bar::SetItemAttached( QQuickItem &inItem ) {
foo = &inItem;
}
//************************************************
//* When this method is called I want to emit a
//* signal on foo, not on the bar instance.
//************************************************
void Bar::BeforeRender() {
// I really want something like foo.emit(...)
emit huzzah();
}
Foo::Foo() : bar( *new Bar() ) {
bar.SetItemAttached( *this );
}
Foo::~Foo() {
delete &bar;
}
How can I modify the code in BeforeRender()
method above to emit a signal on foo
?
The (new-to-QML) C++ programmers around me say that I must have Bar
emit a dummy signal, and then connect it to a slot on Foo
that emits a signal on Foo
. Is this the only (or best) way?
Upvotes: 0
Views: 423
Reputation: 2296
Update the class Bar
. Instead of QQuickItem *foo;
use Foo *foo;
.
class Bar: public QObject {
...
private:
Foo *foo;
};
//and emit the signal
void Bar::BeforeRender() {
emit foo->huzzah();
}
Upvotes: 1