【问题标题】:How to get slot to bind function to signal?如何让插槽绑定函数到信号?
【发布时间】:2013-09-10 19:03:41
【问题描述】:

我正在使用信号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

【问题讨论】:

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


    【解决方案1】:

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

    虽然 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多