【问题标题】:How to override signal handler of superclass component如何覆盖超类组件的信号处理程序
【发布时间】:2015-01-17 06:32:59
【问题描述】:

我有一个类似这样的基类项目:

基础.qml:

Item {
    Thing {
      id: theThing;

      onMySignal: { console.log("The signal"); }   
     }
}

我正在尝试制作一个派生项目 - Derived.qml

如何覆盖theThingonMySignal 处理程序?我试过这样的东西......

派生.qml:

Base {
    theThing.onMySignal: { console.log("Do different things with theThing in Derived") }
}

但我找不到任何东西可以告诉我如何正确地表达这个语法,或者是否/如何实际去做!

【问题讨论】:

    标签: qt inheritance qml handler qt-quick


    【解决方案1】:

    您可以将信号处理程序的代码定义为超类中的属性,并在派生项中覆盖它:

    Item {
        property var handlerCode: function () { console.log("In Superclass") };
    
        Thing {
          id: theThing;
    
          onMySignal: handlerCode() 
         }
    }
    

    覆盖:

    Base {
        handlerCode: function () { console.log("In Derived Item!") };
    }
    

    【讨论】:

    • 或者即使不添加属性...只需在基中定义一个函数,您就可以在派生中创建一个具有相同名称的新函数来覆盖它...所以只需onSignal: callBackFunction()在 Base 中并在 Base 和您想要的派生组件中定义简单的 callBackFunction()
    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2010-10-09
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多