2014-12-19 3 views
5

신호를 인수없이 .disconnect()를 호출하는 것은 불가능 :Qt5 QML의 신호에서 모든 슬롯을 분리 할 수 ​​있습니까? QML에서

file:mainwindow.qml:107: Error: Function.prototype.disconnect: no arguments given 

그래서 어떻게 그들 각각을 지정하지 않고 모든 슬롯을 분리 할 수 ​​있습니까? 아니면 신호 객체를 C++에 전달하고 어떻게 든 연결을 끊을 수 있습니까? 또는 해결 방법이 있습니까?

도달하고자하는 목표는 신호에 다른 슬롯을 연결하여 개체의 동작을 변경하는 것입니다. 예를 들어 :

Item { 
    property var fire 

    // Any qml object. In this example it is ActionExecutor which emits actionRequest 
    ActionExecutor { 
     //signal actionRequest(int actionType) 
     onActionRequest: fire(actionType) 
    } 

    Action { 
     shortcut: "Ctrl+S" 
     text: "One action" 
     onTriggered: { 
      parent.fire = function(actionType) { 
       console.log('one slot') 
      } 
     } 
    } 

    Action { 
     shortcut: "Ctrl+X" 
     text: "Another action" 
     onTriggered: { 
      parent.fire = function(actionType) { 
       console.log('Another slot') 
      } 
     } 
    } 
} 

그래서 그 객체를 재 할당 할 수 있습니다 JS : 내부에서 JSObject를 호출하는 하나 개의 신호에 한 번만 연결 :

object.disconnect() // disconnect all slots 
object.connect(one_super_slot) 
object.disconnect() // disconnect all slots 
object.connect(another_super_slot) 

답변

1

좋아 5 분 내 질문에 후 내가 해결 방법을했습니다 원하는만큼 여러 번이 객체를 재 할당하여 동작을 변경할 수 있습니다. 모든 단순 연결을 끊으려면 undefinedfire으로 지정하십시오.

Item { 
    property var fire 
    property var slots: [ 
     function(actionType) { 
      console.log('1: ' + actionType) 
     }, 

     function() { 
      console.log('2: ' + actionType) 
     }, 

     function() { 
      console.log('3: ' + actionType) 
     } 
    ] 

    // Any qml object. In this example it is ActionExecutor which emits actionRequest 
    ActionExecutor { 
     //signal actionRequest(int actionType) 
     onActionRequest: fire(actionType) 
    } 

    Action { 
     shortcut: "Ctrl+S" 
     text: "One action" 
     onTriggered: { 
      parent.fire = function(actionType) { 
       console.log('calling all custom JS-slots') 

       for (var i in slots) { 
        slots[i](actionType) 
       } 
      } 
     } 
    } 
} 

그래서 사람이 간단한 자바 스크립트 관찰자 패턴으로 QML에서 자신의 신호 슬롯 아키텍처를 구현할 수 있습니다 또한 당신은 같은 것으로 수정 코드에 의해 "슬롯을"체인을 만들 수 있습니다. 즐기십시오.

4

번호 나는 qv4objectwrapper.cpp에 소스 코드를보고, 당신은이 코드를 볼 수 있습니다

void QObjectWrapper::initializeBindings(ExecutionEngine *engine) 
{ 
    engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("connect"), method_connect); 
    engine->functionClass->prototype->defineDefaultProperty(QStringLiteral("disconnect"), method_disconnect); 
} 

사람들은 추가 된 두 개의 방법이 있습니다합니다. method_disconnect()의 소스 코드를 보면 연결을 끊을 슬롯의 이름을 포함하여 항상 하나 또는 두 개의 매개 변수가 필요하다는 것을 알 수 있습니다.

불행히도 disconnectAll()가 없습니다.