Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My class "ComponentArea" looks like this:
C++
#ifndef COMPONENTAREA_H
#define COMPONENTAREA_H

#include <QObject>
#include <QWidget>
#include <QScrollArea>
#include <QtDesigner/QtDesigner>
#include <QMouseEvent>
#include <QPainter>

#include <Components/Sockets/socket.h>
class Socket;

class ComponentArea : public QScrollArea
{
    Q_OBJECT

    public:
        ComponentArea(QWidget* parent);

        void connectSockets(Socket* a, Socket* b);
        void childBlock_childSocket_mousePressEvent(Socket* sender, QMouseEvent* event);
        void childBlock_childSocket_mouseReleaseEvent(Socket* sender, QMouseEvent* event);

    private:
        Socket* pressedSocket;

        void mouseReleaseEvent(QMouseEvent *event);
};

#endif // COMPONENTAREA_H


In the constructor, I set pressedSocket to 0:
C++
pressedSocket = 0;


Whenever I try to use pressedSocket, in one of the events, I get a Segmentation Fault:
C++
void ComponentArea::childBlock_childSocket_mousePressEvent(Socket *sender, QMouseEvent *event)
{
    if((pressedSocket == 0) && (event->button() == Qt::LeftButton)) //SIGSEGV here
    {
        pressedSocket = sender;
    }
}


When I try to inspect pressedSocket at this line, the Debugger tells me "pressedSocket <no such value>".

I don't see what I am doing wrong. Any advices? Thanks in advance!
Posted

My guess is that your ComponentArea object does not exist (yet). Take a look where you allocate that object. Hence, the this pointer is probably pointing into nirwana and then the access to any member will cause a segmentation fault.
 
Share this answer
 
Comments
Chick3nN00dleS0up 27-Mar-15 10:17am    
Well, I promoted a QScrollArea to the ComponentArea in the Qt-Designer. When the application is launched, I am able to see the area and its contents. Also, a breakpoint in the constructor is hit on launching, so I'd say the object exists.
nv3 27-Mar-15 14:01pm    
Try to set a breakpoint at the begin of the childBlock_childSocket_mousePressEvent function. Then take a look at the this pointer. My guess is that it is either 0 or points to an invalid location.
Finally I figured out that a reference to the ComponentView in another class, from which I called the event, was not assigned. Therefore, I was able to call the event, but there was no pressedSocket existent.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900