简体   繁体   中英

Emit a signal on a QQuickItem reference

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM