Qt when calling connect gives me an error

0

I'm having problem with connect. My problem is: I have a class called eventos (events) that will manage all the objects events (buttons, lists etc...) but when I call an event, it gives me an memory reading error. Here is my code:

My main class:

#include "ratagbc.h"
#include "eventos.h"

RataGBC::RataGBC(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    eventos e;
    connect(ui.runBtn,SIGNAL(clicked()),this,SLOT(e.on_runBtn_clicked()));
    ui.setupUi(this);
}

RataGBC::~RataGBC()
{

}

eventos.h:

#include QDialog.h
#include "ui_ratagbc.h"

class eventos : public QDialog, public Ui::RataGBCClass { 
    Q_OBJECT    
public:     
        eventos(void);
    ~eventos(void);

    public slots:       void on_runBtn_clicked(); };

eventos.cpp:

#include "eventos.h"
#include <QMessageBox>


eventos::eventos(void)
{
}


eventos::~eventos(void)
{
}

void eventos::on_runBtn_clicked()
{
    QMessageBox qb;
    qb.setText("Rata");
    qb.exec();
}

The error is:

Unhandled exception at 0x776a15de in RataGBC.exe: 0xC0000005: Access violation reading location 0x00000964.

Debugging I realized (or guessed?) that the error seems to be in the SINGAL call.

c++
debugging
qt
asked on Stack Overflow Feb 29, 2012 by Leonardo • edited Feb 27, 2013 by RafaƂ Rawicki

3 Answers

2

The object eventos e has scope and lifetime limited by a constructor body. You should make it as a member of RataGBC; or inherit it from QObject (I don't see eventos.h, but I believe you have already inherited it as far as you use signal-slot mechanism for it) and create it dymamically as a child of RataGBC, like this

eventos * e = new eventos();
e->setParent(this);

or don't make it child, but just delete it in the destructor of the RataGBC

answered on Stack Overflow Feb 29, 2012 by Tim Kachko • edited Feb 29, 2012 by Tim Kachko
1

your connect method is..

connect(ui.runBtn,SIGNAL(clicked()),this,SLOT(e.on_runBtn_clicked()));

it means that whenever button clicked signal is emitted then for that signal qt will search for the slot in RataGBC class which is not having any such slot. for this to avoid u should replace the current receiver object i.e this by eventos object, as shown below..

connect(ui.runBtn,SIGNAL(clicked()),e,SLOT(e.on_runBtn_clicked()));

this will work for you..

answered on Stack Overflow Feb 29, 2012 by shofee
0

Try following code , your eventos e , object has scope limited to constructor and second you are using this as target object for slot, but you want e to be target. Also you should make e a class member variable and delete that in destructor.

eventos *e = new eventos();
connect(ui.runBtn,SIGNAL(clicked()),e,SLOT(on_runBtn_clicked()));
ui.setupUi(this);
answered on Stack Overflow Feb 29, 2012 by Kunal

User contributions licensed under CC BY-SA 3.0