[英]Emit a signal on a QQuickItem reference
我有一个C ++类Bar
,其中包含对另一个类Foo
的实例的引用。 当调用Bar
上的方法时,我想在Foo
实例上发出QML信号。 我已经20年没有用C ++编写代码了,而QML用来发出信号的语法使我感到困惑。
oo
#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;
}
如何修改上面的BeforeRender()
方法中的代码以在foo
上发出信号?
(新到QML)C ++我身边的程序员说,我必须有Bar
发出虚假信号,然后将其连接到一个插槽Foo
发出的信号Foo
。 这是唯一(或最佳)方法吗?
更新class Bar
。 代替QQuickItem *foo;
使用Foo *foo;
。
class Bar: public QObject {
...
private:
Foo *foo;
};
//and emit the signal
void Bar::BeforeRender() {
emit foo->huzzah();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.