Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsScene subclassing prevents app to quit

QGraphicsScene subclassing prevents app to quit

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gorotoro
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gorotoro
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved