PDA

View Full Version : Program crashes when connecting custom signal.



hersheyzombie
3rd September 2010, 22:29
I have a mainwindow with a textedit widget.
I would like my mainwindow to have an instance of a custom class, called "scene" that has signals and slots connected to ui items, like my textedit widget.

I create my scene as follows:

scene.h


class scene : public QObject
{
Q_OBJECT

public:
scene();

signals:
void setImName(const QString&);
};


scene.cpp


#include "scene.h"

scene::scene() : QObject()
{
emit setImName( tr("hello") );
}


In my mainwindow code, I declare a scene called "myScene" and attempt to connect it to the slot for my textedit widget. This compiles without error but results in a memory access related crash when I run my program. Commenting the "connect" line allows my program to run (but of course not behave as desired).


connect(myScene, SIGNAL(setImName(const QString&)), ui->textEdit1, SLOT(setText(QString)));


Any ideas what I am doing wrong?
I would appreciate any example where someone has created their own QObject with signals and slots that connect to ui widgets...

Lykurg
3rd September 2010, 22:33
I guess the problem is the order of your statements. Can you show us the constructor of your main window.

hersheyzombie
3rd September 2010, 22:54
That was it!

I was declaring my scene after the connect statement.

Thanks.

ashishsaryar
4th September 2010, 10:51
Hi

I think there is problem of connect statement..
1.connect(myScene, SIGNAL(setImName(const QString&)), ui->textEdit1, SLOT(setText(QString)));

It should be like this..
1.connect(myScene, SIGNAL(setImName(const QString&)), ui->textEdit1, SLOT(setText(const QString&)));
I hope it will work.

Lykurg
4th September 2010, 10:54
I hope it will work.As hersheyzombie already said, the order was the problem and it doesn't make any difference if your write
SLOT(setText(QString))or
SLOT(setText(const QString&)).

ashishsaryar
4th September 2010, 11:00
May be crash will happen by ui->textedit...

Just check ui has got memory or not.