gpt4 book ai didi

Ruby 的子类化和 Qt 的信号/槽不能按预期一起工作

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:14 25 4
gpt4 key购买 nike

当尝试在不止一个继承级别上使用 Qt 的信号/槽机制时,我遇到了一个问题:当我的类不直接从 QObject 继承时,信号和槽似乎不再起作用。

以下程序的输出说明了这种情况:

require 'Qt'

class A < Qt::Object
signals 'mySignal()'
slots 'mySlot()'

def initialize
super()
puts "This is the c'tor of A and I am a #{self.class}"
connect(self, SIGNAL('mySignal()'), self, SLOT('mySlot()'))
emit mySignal()
end

def mySlot
puts "Signal received and I am a #{self.class}"
end
end

class B < A
def initialize
super()
end
end

app = Qt::Application.new(ARGV)
A.new
B.new
app.exec

程序产出

This is the c'tor of A and I am a ASignal received and I am a AThis is the c'tor of A and I am a B

不过,我希望

This is the c'tor of A and I am a ASignal received and I am a AThis is the c'tor of A and I am a BSignal received and I am a B

Qt' documentation声明,它“[...] 假定第一个继承的类是 QObject 的子类。”。由于 B < A < QObject,我希望这是真的。相应的 C++ 程序按预期运行(尽管您无法在 C++ 的 c'tor 中识别对象的类型,但这不是这里的重点)。

问题是:为什么程序没有给出预期的输出?

最佳答案

为了能够利用信号和槽——或者更重要的是 Qt 中的元对象系统,该类必须继承自 QObject——并且它必须在多重继承中首先继承 QObject。另见 http://doc.trolltech.com/4.4/moc.html阅读元对象系统

关于Ruby 的子类化和 Qt 的信号/槽不能按预期一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/298064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com