【问题标题】:Qt Program crashes on right clickQt程序在右键单击时崩溃
【发布时间】:2011-10-16 14:03:53
【问题描述】:

我正在为一个大学项目制作视频编辑器。 guiVideoElement(黑色区域)是 guiVideoTrack(浅灰色区域)上视频素材的图形表示。使用 Shift+鼠标左键,您可以在 guiVideoElement 上进行选择(蓝色区域)。要使用选择,您可以通过右键单击所选区域来打开上下文菜单。

正如this tutorial 中所建议的,我使用 contextMenuEvent 打开上下文菜单。不幸的是,整个程序崩溃并显示“程序意外完成。”消息,除非我还定义了 mousePressEvent。即使 mousePressEvent 方法为空(见下文),这也会有所帮助。

这是我的选择代码:

#include "guiselection.h"

#include <QMouseEvent>
#include <QMenu>
#include <QDebug>

GuiSelection::GuiSelection(QWidget *parent, int pos) :
    QLabel(parent)
{
    this->setFixedSize(1,60);
    this->setScaledContents(true);
    this->setPixmap(QPixmap(":/track/gui_selection"));

    this->move(pos, this->pos().y());
    this->show();
}

void GuiSelection::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu(this);

    exampleAct = new QAction(tr("&cut"), this);
    connect(exampleAct, SIGNAL(triggered()), this, SLOT(doSth()));
    menu.addAction(exampleAct);

    menu.exec(ev->globalPos());
}

void GuiSelection::doSth()
{
    qDebug() << "do sth executed";
}

void GuiSelection::mousePressEvent(QMouseEvent *ev) { }

在处理 guiVideoElement 本身的 mousePressEvents 时,在 GuiSelection 类中引入鼠标按钮处理之前,我有相同的行为。执行下面的代码同时注释掉 GuiSelection 中的 contextMenuEvent 和 mousePressEvent 函数将使程序在打印“右键单击”和 parentWidget->width()之后崩溃。 IE。程序在执行完 mousePressEvent 函数中的所有代码后崩溃。

#include "guivideoelement.h"
#include "tracks/videoelement.h"
#include "uitracks/guiselection.h"
#include "uitracks/guivideotrack.h"

#include <QPixmap>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QApplication>

#include <QDebug>

GuiVideoElement::GuiVideoElement(GuiVideoTrack *parent, VideoElement *ve, int length) :
    GuiTrackElement(parent)
{
    ...
}

void GuiVideoElement::mousePressEvent(QMouseEvent *ev)
{
    if(guiSelection != NULL) {
        delete guiSelection;
        guiSelection = NULL;
    }

    if(ev->button() & Qt::LeftButton && QApplication::keyboardModifiers() & Qt::ShiftModifier)
    {
        guiSelection = new GuiSelection(this, ev->pos().x());
    } else if(ev->button() & Qt::RightButton)
    {
        qDebug() << "right button clicked";
    }
    else {
        lastX = this->pos().x();
        lastStableX = this->pos().x();

        // for exact distinction of position us global positions!
        prevMousePos = mapFromGlobal(ev->globalPos()).x();
    }
    qDebug() << parentWidget->width();
}

void GuiVideoElement::mouseMoveEvent(QMouseEvent *ev)
{
    ...
}

...

知道我做错了什么吗?我正在运行 Ubuntu 11.04。其他运行 Windows 的团队成员告诉我,程序不会为他们崩溃,当向左或向右单击时,选择就会消失。对我来说,当我左键单击选择时,什么都没有发生。

【问题讨论】:

  • 您是否在构造函数中将 guiSelection 指针初始化为 NULL?

标签: qt crash click contextmenu mouseevent


【解决方案1】:

我的一个队友发现使用

guiSelection->deleteLater();

而不是

delete guiSelection;

解决问题。 Qt 文档也指出了这个方向(例如,参见 herehere)。

【讨论】:

    【解决方案2】:

    在跟踪程序崩溃时,花时间学习如何使用debugger 真的很值得。这将允许您单步执行程序的代码,并做一些有用的事情,例如查看变量的值。如果程序崩溃,您将能够看到“调用堆栈”(即调用函数的顺序,以到达当前位置)。

    【讨论】:

    • 我确实使用了调试器,但在这种情况下并没有发现它太有用。尤其是调用堆栈并没有真正帮助我,但也许那是因为我是 C++ 初学者。
    • 对了。可能值得在此处发布调用堆栈,然后,如果您可以复制并粘贴它,因为它会提供更多信息,以便其他人帮助您跟踪它。
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2017-06-15
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多