I have a class Subclass, subclassed from QObject. I want to know when the item is deleted, so I connected this slot to the QWidget::destroyed() signal inherited by Subclass. But when I try to cast the argument to with qobject_cast, I get a zero result. The same result obtains from C++'s dynamic_cast. Why?
我有一个类子类,来自QObject。我想知道该项目何时被删除,所以我将这个插槽连接到QWidget::销毁()被子类继承的信号。但是当我尝试用qobject_cast来抛出参数时,得到的结果是零。从c++的dynamic_cast中得到了相同的结果。为什么?
void MyClass::mySlot( QObject * item )
{
qobject_cast<Subclass*>(item); // returns zero, even though item is a Subclass*
}
1 个解决方案
#1
4
The reason is that by the time QObject::destroyed() is emitted, your derived class Subclass has already been destroyed. This is implied by the C++ order of destruction. Also, this question deals with a similar issue.
原因是,在QObject::销毁()的时候,您的派生类子类已经被销毁了。这是由C++的破坏顺序所暗示的。同样,这个问题也涉及到一个类似的问题。
To get around this, you could either use C-style pointer casting (which is dispreferred), or rewrite your code to use a QObject instead.
为了解决这个问题,您可以使用c样式的指针转换(这是不需要的),或者重写您的代码来使用一个QObject。
#1
4
The reason is that by the time QObject::destroyed() is emitted, your derived class Subclass has already been destroyed. This is implied by the C++ order of destruction. Also, this question deals with a similar issue.
原因是,在QObject::销毁()的时候,您的派生类子类已经被销毁了。这是由C++的破坏顺序所暗示的。同样,这个问题也涉及到一个类似的问题。
To get around this, you could either use C-style pointer casting (which is dispreferred), or rewrite your code to use a QObject instead.
为了解决这个问题,您可以使用c样式的指针转换(这是不需要的),或者重写您的代码来使用一个QObject。