我遇到一些QObject的问题,当我不期望它被删除时。
我有这门课(注意我没有宣布Q_OBJECT)
class MyEntry : public QTreeWidgetItem
{
public:
MyEntry(QTreeWidgetItem* _parent, QString _name = "noname") :
QTreeWidgetItem(_parent, QStringList() << _name) {}
... it holds a few QStrings and has some setter/getter methods for them
};
我在包含QTreeWidget的类中创建并使用此对象。偶尔这个类将其MyEntries中的一个“发送”到另一个类,该类使用MyEntry中的信息来发送它以保存它所保存的一些QLabel上的文本。我使用以下QEvent:
class MyEvent : public QEvent
{
public:
MyEvent(MyEntry* _ob) : QEvent((QEvent::Type)MYEVENT1), mp_ob(_ob) {}
~MyEvent() {}
MyEntry* getOb() { return mp_ob; }
private:
MyEntry* mp_ob;
};
我像这样发送和接收此事件(接收者是parent(),lp_currentEntry是MyEntry对象):
Class that contains the QTreeWidget:
QApplication::postEvent(parent() , new MyEvent(lp_currentEntry));
Class that contains the QLabels:
void MyOtherClass::customEvent(QEvent* _p_ev)
{
return;
}
我遇到的问题是,一旦事件发布,MyEvent就会被删除。但它所包含的MyEntry的析构函数也被称为。我从未使用MyEvent作为父级创建MyEntry,为什么它被删除?
我正在使用这个设置,因为postEvent通常由一个不是ui的线程完成,我很确定非ui线程不应该更新QLabel等,否则我可以简单地完成(MyOtherClass*)parent()->updateLabels(lp_currentEntry);
- 但如果我错了,请在那里纠正。
修改:这是一个回溯:
#0 MyEntry::~MyEntry (this=0x7f83a0, __in_chrg=<optimized out>) at ../jb2/containers.cpp:171
#1 0x000000000040ede9 in MyEntry::~MyEntry (this=0x7f83a0, __in_chrg=<optimized out>) at ../jb2/containers.cpp:178
#2 0x0000000000416439 in ~MyEvent (this=0xfc7810, __in_chrg=<optimized out>) at ../jb2/jb2Events.h:28
#3 MyEvent::~MyEvent (this=0xfc7810, __in_chrg=<optimized out>) at ../jb2/jb2Events.h:28
#4 0x0000003ad317b45a in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /lib64/libQtCore.so.4
#5 0x0000003ad31a5e33 in ?? () from /lib64/libQtCore.so.4
#6 0x0000003b24847825 in g_main_context_dispatch () from /lib64/libglib-2.0.so.0
#7 0x0000003b24847b58 in ?? () from /lib64/libglib-2.0.so.0
#8 0x0000003b24847c14 in g_main_context_iteration () from /lib64/libglib-2.0.so.0
#9 0x0000003ad31a5fc6 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#10 0x0000003ad4a6a5ee in ?? () from /lib64/libQtGui.so.4
#11 0x0000003ad31766ef in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#12 0x0000003ad3176978 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#13 0x0000003ad317b768 in QCoreApplication::exec() () from /lib64/libQtCore.so.4
#14 0x0000000000408f53 in main (argc=1, argv=<optimized out>) at ../jb2/main.cpp:10