开发者

Qt Program crashes on right click

I'm working on a video editor for a university project. The guiVideoElement (black area) is the graphic representation of video material on a guiVideoTrack (light grey area). With Shift+left mouse button, you can make a selection (blue area) on the guiVideoElement. To work with the selection, you can open a context menu by right clicking on the selected area.

Qt Program crashes on right click

As suggested in this tutorial, I use the contextMenuEvent to open the context menu. Unfortunately, the whole program crashes with the message "The program has unexpectedly finished.", unless I also define the mousePressEvent. This helps even if the mousePressEvent method is empty (see below).

Here's my code for the selection:

#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) { }

I had the same behaviour before introducing the mouse button handling in the GuiSelection class when handling mousePressEvents on the guiVideoElement itself. Executing the code below while commenting out the contextMenuEvent and mousePressEvent functions in the GuiSelection will make the program crash after printing "right button clicked" and parentWidget->width(). I.e. the program crashes after having executed all the code in the mousePressEvent function.

#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 i开发者_如何学Cf(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)
{
    ...
}

...

Any idea what I am doing wrong? I'm running Ubuntu 11.04. Other team members, who run Windows, tell me that the program does not crash for them, the selection simply disappears when clicking on it either left or right. For me, when I left click on the selection, nothing happens at all.


A team mate of mine found that using

guiSelection->deleteLater();

instead of

delete guiSelection;

solves the problem. The Qt docs also point to that direction (e.g. see here and here).


When tracking down program crashes, it's really worth spending the time to learn how to use a debugger. This will allow you to step through the program's code, and do useful things like see the values of variables. And if the program crashes, you'll be able to see the 'call-stack' (that is, the order in which functions were called, to get to the current location).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜