我有一个C++类Bar,它包含对另一个类Foo实例的引用。当调用Bar上的方法时,我希望在Foo实例上发出QML信号。我已经20年没有用C++编写代码了,QML用来发出信号的语法让我感到困惑。
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;
}如何修改上述BeforeRender()方法中的代码以在foo上发出信号
我周围的(新到QML) C++程序员说,我必须让Bar发出一个虚拟信号,然后将它连接到Foo上一个在Foo上发射信号的插槽。这是唯一(或最好的)方式吗?
发布于 2016-07-20 09:46:03
更新class Bar。使用QQuickItem *foo;代替Foo *foo;。
class Bar: public QObject {
...
private:
Foo *foo;
};
//and emit the signal
void Bar::BeforeRender() {
emit foo->huzzah();
}https://stackoverflow.com/questions/38467895
复制相似问题