QGraphicsScene subclassing prevents app to quit



  • Hi all,

    I am a Qt newbie, i am writing application with ability to select regions of images.
    I subclassed QGraphicsScene to create AlignmentPointSelectionScene/

    The problem with the application is that if I only call
    @AlignmentPointSelectionScene myScene(testImage);@

    then application does not fully quit. Main window closes, but the process remains running -
    it is seen in TaskManager. The destructor is not called.

    Tried signals QApplication::aboutToQuit etc... - no result.

    Actually creating an object of the even empty QGraphicsScene subclass also prevent closing my application.

    Please help me.

    The code is

    @#ifndef ALIGNMENTPOINSSELECTIONSCENE_H
    #define ALIGNMENTPOINSSELECTIONSCENE_H
    #include <QGraphicsScene>
    #include <QList>
    class QImage;
    class QPixmap;

    class AlignmentPointSelectionScene : public QGraphicsScene
    {
    Q_OBJECT

    public:
    QList<QRectF *> SelectRectangles;
    AlignmentPointSelectionScene(const QImage& imageForSelection, QObject *parent = 0);
    AlignmentPointSelectionScene( QObject *parent = 0);
    ~AlignmentPointSelectionScene();

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void keyPressEvent(QKeyEvent *keyEvent);

    private:
    bool SelectionStarted;
    qreal startX;
    qreal startY;
    QPixmap * selectPixmap;

    };

    #endif // ALIGNMENTPOINSSELECTIONSCENE_H
    @

    and the implementation is
    @
    #include <QtGui>
    #include "AlignmentPoinsSelectionScene.h"
    #include <QImage>
    #include <QPixmap>
    #include <QColor>

    AlignmentPointSelectionScene::AlignmentPointSelectionScene(const QImage& imageForSelection, QObject *parent)
    : QGraphicsScene(parent)
    {

    qreal sceneWidth=imageForSelection.width();
    qreal sceneHeight=imageForSelection.height();
    this->setSceneRect(0,0,sceneWidth,sceneHeight);
    selectPixmap = new QPixmap(sceneWidth,sceneHeight);
    selectPixmap->convertFromImage(imageForSelection.mirrored());
    this->addPixmap(* selectPixmap);
    SelectionStarted=false;
    

    }

    AlignmentPointSelectionScene::AlignmentPointSelectionScene( QObject *parent)
    : QGraphicsScene(parent)
    {
    SelectionStarted=false;

    }

    AlignmentPointSelectionScene::~AlignmentPointSelectionScene()
    {
    while(this->items().count()>1)
    {
    this->removeItem(this->items().first());

    }
    delete selectPixmap;
    

    }

    void AlignmentPointSelectionScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    if (mouseEvent->button() == Qt::LeftButton)
    {
    SelectionStarted=true;
    startX=mouseEvent->scenePos().x();
    startY=mouseEvent->scenePos().y();
    }
    QGraphicsScene::mousePressEvent(mouseEvent);
    }

    void AlignmentPointSelectionScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
    if (SelectionStarted)
    {
    SelectionStarted=false;
    qreal selection_width=mouseEvent->scenePos().x()-startX;
    qreal selection_height=mouseEvent->scenePos().y()-startY;
    QRectF selectionRectF(startX, startY, selection_width,selection_height);
    QPen selectPen(Qt::DashLine);
    QColor selectColor(0,255,0);
    selectPen.setColor(selectColor);
    this->addRect(selectionRectF,selectPen);
    }
    QGraphicsScene::mousePressEvent(mouseEvent);
    }

    void AlignmentPointSelectionScene::keyPressEvent(QKeyEvent *keyEvent)
    {

    if(!SelectionStarted)
    {
        if(keyEvent->key()==Qt::Key_Backspace)
        {
            if (this->items().count()>1 )
            {
                this->removeItem(this->items().first());
                this->invalidate();
            }
        }
    }
    

    }

    @

    The problem with the application is that if I only call
    @AlignmentPointSelectionScene myScene(testImage);@

    then application does not fully quit. Main window closes, but the process remains running -
    it is seen in TaskManager. The destructor is not called.

    Tried signals QApplication::aboutToQuit etc... - no result.

    Actually creating an object of the even empty QGraphicsScene subclass also prevent closing my application.

    Please help me.



  • The problem was solved by creating Exit menu item and assigning "Alt+F4" key to it. I don't know why, because I previously used mouse and cross icon on the window to close, and do the same now.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.