0

当我不希望它被删除时,我遇到了一些问题。

我有这个类(注意我没有声明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 的信息来设置它持有的一些 QLabels 上的文本。我使用以下 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 线程不应该更新 QLabels 等,否则我可以简单地完成(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
4

0 回答 0