在调试版本上运行在发行版本上失败

时间:2019-03-01 12:27:46

标签: c++ qt

我尝试在Linux平台(Ubuntu 18.04)上使用拖放操作在Qt 5.12.0中创建一个简单的应用程序。有问题。

如果我在调试器(gdb)中启动它,它将毫无问题地运行。如果我在没有调试器的情况下启动它并在执行过程中附加了它,或者只是在没有附加调试器的情况下运行了它,则会出现Segmentation fault (core dumped)错误。这是源代码:

widget.h

#ifndef MY_WIDGET_H
#define MY_WIDGET_H

#include <QWidget>

class QMouseEvent;
class QDragEnterEvent;
class QDragMoveEvent;
class QDragLeaveEvent;
class QDropEvent;

class Widget : public QWidget
{

    Q_OBJECT

  public:
    Widget(QWidget *parent = nullptr);

  protected:
    void mousePressEvent(QMouseEvent *event) override;
    void dragEnterEvent(QDragEnterEvent *event) override;
    void dragMoveEvent(QDragMoveEvent *event) override;
    void dragLeaveEvent(QDragLeaveEvent *event) override;
    void dropEvent(QDropEvent *event) override;
};

#endif

widget.cpp

#include "widget.h"

#include <QMimeData>
#include <QDrag>
#include <QDropEvent>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setAutoFillBackground(true);
    setLayout(new QVBoxLayout());
    setAcceptDrops(true);
    setMinimumSize(50, 50);
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    QMimeData *mime = new QMimeData();
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mime);
    this->hide();
    drag->exec(Qt::MoveAction);
    this->show();
}

void Widget::dragEnterEvent(QDragEnterEvent *event)
{
    event->accept();
}

void Widget::dragLeaveEvent(QDragLeaveEvent *event)
{
    event->accept();
}

void Widget::dragMoveEvent(QDragMoveEvent *event)
{
    event->accept();
}

void Widget::dropEvent(QDropEvent *event)
{
    event->setDropAction(Qt::MoveAction);
    event->accept();
    QWidget *widget = qobject_cast<QWidget *>(event->source());
    layout()->addWidget(widget);
}

main.cpp

#include <QApplication>

#include "widget.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    Widget wdg1;
    Widget wdg2;
    wdg1.show();
    wdg2.show();
    QPalette pal;
    pal.setColor(QPalette::Background, Qt::red);
    wdg1.setPalette(pal);
    pal.setColor(QPalette::Background, Qt::blue);
    wdg2.setPalette(pal);

    return app.exec();
}

proj.pro

######################################################################
# Automatically generated by qmake (3.1) Wed Feb 27 12:57:15 2019
######################################################################

TEMPLATE = app
TARGET = proj
INCLUDEPATH += .

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
HEADERS += widget.h
SOURCES += main.cpp widget.cpp

QT += core gui widgets

执行后,将一个小部件拖放到另一个小部件上以重现错误。

在类似的项目中,我遇到了相同的错误,该错误是从

引发的
#0  0x00007ffff6f764f2 in QCoreApplication::postEvent(QObject*, QEvent*, int) (receiver=receiver@entry=0x555555a42d30, event=event@entry=0x555555a42f50, priority=priority@entry=0) at kernel/qcoreapplication.cpp:1503
#1  0x00007ffff5f13ec7 in QBasicDrag::eventFilter(QObject*, QEvent*) (this=<optimized out>, o=0x555555a42d30, e=0x7fffffffca80) at kernel/qsimpledrag.cpp:187
#2  0x00007ffff7ec42b9 in QXcbDrag::eventFilter(QObject*, QEvent*) (this=<optimized out>, o=<optimized out>, e=<optimized out>) at qxcbdrag.cpp:170
#3  0x00007ffff6f6f8ac in QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject*, QEvent*) (this=this@entry=0x5555557721d0, receiver=receiver@entry=0x555555a42d30, event=event@entry=0x7fffffffca80)
    at kernel/qcoreapplication.cpp:1168
#4  0x00007ffff742feac in QApplicationPrivate::notify_helper(QObject*, QEvent*) (this=this@entry=0x5555557721d0, receiver=receiver@entry=0x555555a42d30, e=e@entry=0x7fffffffca80) at kernel/qapplication.cpp:3705
#5  0x00007ffff7437590 in QApplication::notify(QObject*, QEvent*) (this=0x7fffffffdc30, receiver=0x555555a42d30, e=0x7fffffffca80)
    at kernel/qapplication.cpp:3093
#6  0x00007ffff6f6fbe3 in QCoreApplication::notifyInternal2(QObject*, QEvent*) (receiver=0x555555a42d30, event=0x7fffffffca80)
    at kernel/qcoreapplication.cpp:1060
#7  0x00007ffff6f6fe64 in QCoreApplication::sendSpontaneousEvent(QObject*, QEvent*) (receiver=receiver@entry=0x555555a42d30, event=event@entry=0x7fffffffca80)
    at kernel/qcoreapplication.cpp:1462

有人可以帮助我理解问题吗?

0 个答案:

没有答案