我目前正在使用Qt 4.8.5和c ++进行项目。
我创建了一个名为ItemView的自定义小部件,并重新实现了拖放方法。
问题是,它在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
有没有办法使它在发布模式下工作?
谢谢。
最佳答案
data->setText(oss.str().c_str());
从一个临时字符串中construct a new QString possibly backed by一个
char*
(即从setText返回后肯定被销毁)。稍后,当您的程序尝试操作该字符串时,由于编译器的恶作剧,它将在发行时崩溃(因为它无效),但在调试时可能不会崩溃。而是使用
data->setText(QString::fromStdString(oss.str()));
关于c++ - Qt-QDrag执行程序上的Segfault,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22302786/