PDA

View Full Version : Problem with signals slots and emit



slqh
19th September 2011, 02:19
Hello.
I'm currently learning how to program in c++ with qt and I'm having a problem when trying to emit a signal to a second mainwindow.
The whole idea is to emit a signal inside a function in the first form and catch it in the second form to add a line to a textEdit.
The idea is to use the second form as some type of debug console, there i want to show debuggin messages so to avoid having a system console open to show the qDebug messages and to provide a save and report mechanism to the messages.

I wrote this simple example to demostrate my problem.

main.cpp


#include <QtGui>
#include "first.h"
#include "second.h"

int main(int argc, char** argv)
{
QApplication app(argc, argv);

first f;
f.show();

second s;
s.show();

return app.exec();
}


first.cpp


#include <QtGui>

#include "first.h"
#include "second.h"

first::first()
{

QPushButton *button = new QPushButton("First", this);
setCentralWidget( button );


QString text = "test";
connect( button, SIGNAL ( clicked() ) ,this, SLOT( doemit() ));

//if i uncomment the next two lines the program stales and never shows the forms
//second s;
//connect( this, SIGNAL(MySignal(QString)), &s, SLOT( add() ));

}

first::~first()
{}

void first::doemit() {

qDebug() << "doemit called";

QString test = "text";
emit MySignal(test);

}

#include "first.moc"


first.h


#ifndef first_H
#define first_H

#include <QtGui/QMainWindow>

class first : public QMainWindow
{
Q_OBJECT
public:
first();
virtual ~first();

public slots:
void doemit();

signals:
void MySignal( QString );
};

#endif // first_H


second.cpp


#include "second.h"
#include "first.h"

#include <QtGui>
#include <QTextEdit>

second::second() {

textEdit = new QTextEdit;
setCentralWidget(textEdit);

first f;
connect(&f, SIGNAL(MySignal(QString)), this, SLOT(add()));
}

second::~second(){}

void second::add(){
qDebug() << "Slot Called";
textEdit->append ( "Blah" );
}

#include "second.moc"



second.h


#ifndef second_H
#define second_H

#include <QtGui>
#include <QTextEdit>

class second : public QMainWindow
{
Q_OBJECT
public:
second();
virtual ~second();
QTextEdit *textEdit;

public slots:
void add();
};

#endif // second_H


What happens with the program is that doemit() is called in first.cpp but never reaches the slot in second.cpp.
if i try to reverse the connection, as in commenting the connect line in second.cpp and uncommenting the connect in first the same happens
but if i leave both connects the program starts but nothing is show in screen.

Maybe i can't have to mainwindows as one blocks the other and i need to dig into threading, but i would like to avoid that.

I hope that my problem is clear, as I'm not a native english speaker maybe my writing is a little cryptic at best.

Lykurg
19th September 2011, 07:41
The connection has to be established in main where the actual second instance is! It couldn't work any other way...


...and
first f;
connect(&f, SIGNAL(MySignal(QString)), this, SLOT(add())); is nonsense since f gets destroyed at the scope end. (Basic C++)

slqh
19th September 2011, 08:48
The connection has to be established in main where the actual second instance is! It couldn't work any other way...


Thank you.
That was a simple solution, I thought that connect() could only be called from inside the from constructor.
As the examples I read were consistently doing the connects there.


As for the Basic c++ error.
C++ has proved to be a really complex language to learn, at least for me.
As i get better I hope to have less stuff like that or at least leave them out of the forum questions!

Thank you for your time.

Lykurg
19th September 2011, 09:02
Thank you.
That was a simple solution, I thought that connect() could only be called from inside the from constructor.
As the examples I read were consistently doing the connects there. They are doing it there because they are defining the objects there. You can call connect everywhere you like, but you must have valid pointers to the objects you like to connect. In your case you created a new instance of first/second and used that. But this instances aren't identical with that ones you have created in main function and which you actually use.

slqh
19th September 2011, 09:20
They are doing it there because they are defining the objects there. You can call connect everywhere you like, but you must have valid pointers to the objects you like to connect. In your case you created a new instance of first/second and used that. But this instances aren't identical with that ones you have created in main function and which you actually use.

Man not only did you help me with my problem it looks like you can read minds.
i was looking againt at the working code to see exactly what was the root of my problem.
And an after a refresh y see this.

I understand now that i need to pass the same object to the connect than i used to show() the form.
creating a new instance does not work because well is a new instance and not the one i was seeing.

Again. Thank you.

Edit. Also for future reference this is the working way.



MainWindow win;
win.resize(1024, 768);
win.show();

debug deb;
deb.show();
QObject::connect(&win, SIGNAL( MySignal() ),&deb, SLOT( MySlot() ));