Qt - 关于QDrag执行的Segfault

时间:2014-03-10 14:22:17

标签: c++ qt drag-and-drop segmentation-fault

我目前正在使用Qt 4.8.5和c ++进行项目。

我创建了一个名为ItemView的自定义Widget,并重新实现了拖放方法。 问题是,它在QDrag :: exec()上有段错误。 所以我尝试在调试模式下编译,现在它可以工作。

以下是代码:

  std::pair<AItem const *, unsigned int>    *pair = new std::pair<AItem const *, unsigned int>(_item, _nb);
  std::ostringstream       oss;

  oss << pair;
  QDrag         *dr = new QDrag(this);
  QMimeData             *data = new QMimeData;

  data->setText(oss.str().c_str());
  dr->setMimeData(data);
  dr->exec();

这是回溯

#0  0x00007ffff4b33c6a in strlen () from /usr/lib/libc.so.6
#1  0x00007ffff7adc425 in XSetCommand () from /usr/lib/libX11.so.6
#2  0x00007ffff7ae0831 in XSetWMProperties () from /usr/lib/libX11.so.6
#3  0x00007ffff5cc9a07 in QWidgetPrivate::create_sys(unsigned long, bool, bool) () from /usr/lib/libQtGui.so.4
#4  0x00007ffff5c8142d in QWidget::create(unsigned long, bool, bool) () from /usr/lib/libQtGui.so.4
#5  0x00007ffff5c8997a in QWidget::setVisible(bool) () from /usr/lib/libQtGui.so.4
#6  0x00007ffff5cc0bcf in ?? () from /usr/lib/libQtGui.so.4
#7  0x00007ffff5cc1e15 in ?? () from /usr/lib/libQtGui.so.4
#8  0x00007ffff5c4c064 in QDrag::start(QFlags<Qt::DropAction>) () from /usr/lib/libQtGui.so.4
#9  0x000000000041d8da in ItemView::makeDrag (this=0x2e31db0) at src/Qt/Views/ItemView.cpp:155
#10 0x000000000041d9c3 in ItemView::mousePressEvent (this=0x2e31db0, mEvent=0x0) at src/Qt/Views/ItemView.cpp:66
#11 0x00007ffff5c8a4a1 in QWidget::event(QEvent*) () from /usr/lib/libQtGui.so.4
#12 0x00007ffff5c3b0dc in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
#13 0x00007ffff5c417dd in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
#14 0x00007ffff570bebd in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/libQtCore.so.4
#15 0x00007ffff5c40f93 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool) () from /usr/lib/libQtGui.so.4
#16 0x00007ffff5cb2bab in ?? () from /usr/lib/libQtGui.so.4
#17 0x00007ffff5cb161c in QApplication::x11ProcessEvent(_XEvent*) () from /usr/lib/libQtGui.so.4
#18 0x00007ffff5cd8162 in ?? () from /usr/lib/libQtGui.so.4
#19 0x00007ffff2f4a296 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#20 0x00007ffff2f4a5e8 in ?? () from /usr/lib/libglib-2.0.so.0
#21 0x00007ffff2f4a68c in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
#22 0x00007ffff5738b25 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQtCore.so.4
#23 0x00007ffff5cd8216 in ?? () from /usr/lib/libQtGui.so.4
#24 0x00007ffff570ab1f in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQtCore.so.4
#25 0x00007ffff570ae15 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQtCore.so.4
#26 0x00007ffff570ff4b in QCoreApplication::exec() () from /usr/lib/libQtCore.so.4
#27 0x000000000042b041 in WindowManager::exec (this=this@entry=0x733b90) at src/Qt/WindowManager.cpp:66
#28 0x00000000004401c0 in Client::init (this=this@entry=0x729750, ac=ac@entry=1, av=av@entry=0x7fffffffe858) at src/Client.cpp:52
#29 0x0000000000414b04 in main (ac=1, av=0x7fffffffe858) at src/main.cpp:22

有没有办法让这项工作在发布模式下工作?

感谢。

2 个答案:

答案 0 :(得分:0)

data->setText(oss.str().c_str());

construct a new QString possibly backed by来自 临时字符串 char*(即从setText返回后肯定会被破坏)。稍后,当您的程序将尝试操作字符串时,它将在发布时崩溃(因为它无效),但由于编译器的恶作剧,可能无法进行调试。

而是使用

data->setText(QString::fromStdString(oss.str()));

答案 1 :(得分:0)

好的找到了解决方案。我没有在我的main中创建QApplication,所以我必须在我执行它的函数中通过引用传递argc。