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

基础 .qml:

Item {
    Thing {
      id: theThing;

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

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

如何覆盖onMySignaltheThing处理程序?我已经尝试过这样的东西...

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

但是我找不到任何东西可以告诉我如何正确地在语法上表达它,或者是否/如何真正做到这一点!

最佳答案

您可以将信号处理程序的代码定义为父类(super class)中的一个属性,并在派生项中覆盖它:

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

    Thing {
      id: theThing;

      onMySignal: handlerCode()
     }
}

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

关于qt - 如何重写父类(super class)组件的信号处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27996887/

10-10 04:06