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)
{
first f;
f.show();
second s;
s.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
first.cpp
#include <QtGui>
#include "first.h"
#include "second.h"
first::first()
{
setCentralWidget( button );
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";
emit MySignal(test);
}
#include "first.moc"
#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"
To copy to clipboard, switch view to plain text mode
first.h
#ifndef first_H
#define first_H
#include <QtGui/QMainWindow>
{
Q_OBJECT
public:
first();
virtual ~first();
public slots:
void doemit();
signals:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
second.cpp
#include "second.h"
#include "first.h"
#include <QtGui>
#include <QTextEdit>
second::second() {
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"
#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"
To copy to clipboard, switch view to plain text mode
second.h
#ifndef second_H
#define second_H
#include <QtGui>
#include <QTextEdit>
{
Q_OBJECT
public:
second();
virtual ~second();
public slots:
void add();
};
#endif // 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
To copy to clipboard, switch view to plain text mode
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.
Bookmarks