Results 1 to 3 of 3

Thread: overriding mousemoveevent SEGFAULTS

  1. #1
    Join Date
    Sep 2017
    Location
    Montevideo, Uruguay
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default overriding mousemoveevent SEGFAULTS

    Hello, this is my very first post.

    I have moved from other languages to PyQt several years ago and now wish to reimplement several systems on Qt with C++.

    I have stumbled upon a SEGFAULT that I could trace here:

    void QScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene:mouseMoveEvent(event);
    }

    I am in the process of subclassing a QGraphicsScene. Relevant Code below:

    HEADER

    #ifndef QSCENE_H
    #define QSCENE_H
    #include "itemspanel.h"
    #include "QGraphicsScene"
    #include "QtCore/Qt"
    #include "QColor"


    class QScene : public QGraphicsScene
    {
    Q_OBJECT


    public:
    explicit QScene(QGraphicsScene* parent = 0);
    ~QScene();
    ItemsPanel *panel;
    int miModo;
    int modoGoma;
    int modoLinea;
    int modoFluo;
    int modoLineaH;
    int modoRect;
    QPointF inicio;
    QPointF fin;
    int x = 0;
    int y = 0;
    QColor brushColor;
    QColor penColor;
    int penWidth;
    int pen1;
    int pen2;
    int pen3;
    int pen4;
    void fmodoGoma();
    void fmodoRect();
    void fmodoLinea();
    void fmodoLineaH();
    void fmodoFluo();
    void fmodoPen1();
    void fmodoPen2();
    void fmodoPen3();
    void fmodoPen4();
    void fmodoColorRojo();
    void fmodoColorAzul();
    void fmodoColorVerde();
    void fmodoColorNaranja();
    void fmodoColorAmarillo();
    void fmodoColorNegro();
    void guardar();
    void eliminarItemsSeleccionados();
    void levantarCapaTexto(QString);
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    };

    #endif // QSCENE_H

    CODE

    #include "qscene.h"
    #include "QGraphicsScene"
    #include "itemspanel.h"
    #include "QPointF"
    #include "QtCore/Qt"
    #include "QColor"
    #include "QGraphicsTextItem"
    #include "QGraphicsSceneMouseEvent"
    #include "QGraphicsRectItem"
    #include "QGraphicsLineItem"
    #include "QPen"
    #include "QBrush"

    QScene::QScene(QGraphicsScene *parent):
    QGraphicsScene(parent)
    {
    ItemsPanel *panel = 0;
    }

    QScene::~QScene()
    {
    delete panel;
    }

    void QScene::mouseDoubleClickEvent(QGraphicsSceneMouseE vent *event){
    QGraphicsTextItem *txtItem = new QGraphicsTextItem("...");
    txtItem->setPos(event->scenePos());
    txtItem->setTextInteractionFlags(Qt::TextSelectableByMou se | Qt::TextSelectableByKeyboard | Qt::TextEditable);
    txtItem->setFlag(QGraphicsItem::ItemIsMovable);
    txtItem->setFlag(QGraphicsItem::ItemIsSelectable);
    txtItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges );
    txtItem->setFont(QFont("Lucida Console",19));
    this->addItem(txtItem);
    txtItem->setFocus();
    QGraphicsScene::mouseDoubleClickEvent(event);
    }

    void QScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    this->x = event->scenePos().x();
    this->y = event->scenePos().y();
    QGraphicsScene::mousePressEvent(event);
    }
    void QScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene:mouseMoveEvent(event);
    }

    void QScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    if(miModo==modoRect){
    QGraphicsRectItem *rect = new QGraphicsRectItem(x,y,event->scenePos().x()-x,event->scenePos().y()-y);
    QPen pen ;
    pen.setColor(penColor);
    pen.setWidth(penWidth);
    rect->setPen(pen);
    rect->setFlag(QGraphicsItem::ItemIsMovable);
    rect->setFlag(QGraphicsItem::ItemIsSelectable);
    rect->setFlag(QGraphicsItem::ItemSendsGeometryChanges );
    this->addItem(rect);
    miModo = 0 ;
    guardar();
    QGraphicsScene:mouseReleaseEvent(event);
    }


    The mouse move event gets called hundreds of times and finally a segmentation fault occurs. I do not have idea as to why. Can someone provide some clues?

    Regards!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: overriding mousemoveevent SEGFAULTS

    The mouse move event gets called hundreds of times and finally a segmentation fault occurs. I do not have idea as to why. Can someone provide some clues?
    Possibly you have stack overflow due to recursion. I do not think you should call the base class method if you override an event handler. Instead, you should call event->accept() if you process the event yourself or event->ignore() if you are not interestd and want the event processed by the base class in the normal way.

    Please use CODE tags next time you post source code. Se my signature block below for instructions.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: overriding mousemoveevent SEGFAULTS

    Please pay close attention to compiler warning, you will see the problem. Anyway the problem is that you have indefinite recursion.

    Qt Code:
    1. void QScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    2. //QGraphicsScene:mouseMoveEvent(event); //<<<<<<<<< replace with below line, note the "::"
    3. QGraphicsScene::mouseMoveEvent(event);
    4. }
    5.  
    6. void QScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    7. ...
    8. //QGraphicsScene:mouseReleaseEvent(event); //<<<<<<<<< replace with below line, note the "::"
    9. QGraphicsScene::mouseReleaseEvent(event);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QwtPlotPicker Subclass Segfaults
    By npotts in forum Qwt
    Replies: 4
    Last Post: 7th December 2014, 16:59
  2. QStandardItem::appendRow() segfaults
    By i92guboj in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2014, 17:08
  3. QSqlDatabase::open Segfaults
    By rich.remer in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 16:32
  4. How to catch segfaults?
    By Daliphant in forum Newbie
    Replies: 4
    Last Post: 3rd July 2008, 14:17
  5. Anyone an idea why this code segfaults?
    By Kumosan in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2007, 05:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.