如何获取插槽绑定功能信号?

时间:2013-09-10 19:03:42

标签: c++ boost binding const boost-signals2

我正在使用信号2。我正在尝试使用具有订阅插槽的视图设置视图状态/视图关系。我似乎无法触发处理程序功能。绑定有问题吗?我是c ++的新手所以也许是对const / reference / dereferencer的误用。

在我的国家机器中:

void State::setAppState( State::AppState pNewState )
{
    mCurrentState = pNewState;
    // this prints fine
    ci::app::console() <<"\nState::setState " << pNewState << "\n";
    (*mSignal)();
}

在我的视图基类中:

BaseView::BaseView( State& pState ): mState(pState)
{
    // register connection to state
    mConnection = mState.connect(boost::bind(&BaseView::stateChange, this));
}

// the use of const is right from their example in the doc.
// but i found i had to const_cast to get it to compile
// can i get rid of the 'this' and do it without const method? 
// edit: no (boost compile errors)
void BaseView::stateChange()  const
{
    int s = const_cast<State&>(mState).getAppState();
    // this does not print
    ci::app::console() << "\n>>>>> View Slot registered change " << s << "\n" ;
}

在我的视图子类中:

AttractView::AttractView(State pState):BaseView(pState)
{
     // to pass the constructor param
}

主要应用程序:

mAttract = AttractView( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract); //AppState is just an enum

1 个答案:

答案 0 :(得分:0)

我认为问题实际上是初始化列表。

虽然pState在BasiView构造函数中是有效的,但它没有正确绑定,或者在信号初始化和绑定被删除之前绑定了它。

 BaseView::BaseView( State& pState ): mState(pState)

添加额外的init / create函数可以清除它。

void BaseView::init(State& pState){
    mConnection = mState.connect( (boost::bind(&BaseView::stateChange, this)) );
}

...

mAttract.init( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract);