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.
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.