[SOLVED] A slot in my receiving class doen't receive a signal from a push button in MainWindow
-
Following the SignalSlotTesting example "here":http://forum.codecall.net/topic/70716-qt-part2-signal-slot/ worked fine, but I can't seem to make a slot in my Receiver class instance receive a signal from a push button in my MainWindow, when experimenting further as a Gui application. I wonder what's missing...
(Running Qt 4.8.4 with Qt Creator 2.7.1 )In the mainwindow.cpp, everything works fine except for the connection line below:
@ QObject::connect(ui->pushButton, SIGNAL(clicked()), &receiver, SLOT(wee()));@mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();public slots:
void getSig();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H@
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "sender.h"
#include "receiver.h"#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);Sender sender; Receiver receiver; QObject::connect(&sender, SIGNAL(foo(QString)), &receiver, SLOT(bar(QString))); QObject::connect(&sender, SIGNAL(foo(QString)), ui->label, SLOT(setText(QString))); QObject::connect(ui->pushButton, SIGNAL(clicked()), ui->label, SLOT(close())); QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(getSig())); QObject::connect(ui->pushButton, SIGNAL(clicked()), &receiver, SLOT(wee())); sender.fireSignal();
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::getSig()
{
qDebug() << "got signal.";
}@sender.h
@#ifndef SENDER_H
#define SENDER_H#include <QObject>
class Sender : public QObject
{
Q_OBJECT
public:
explicit Sender(QObject *parent = 0);
void fireSignal();signals:
void foo(QString);public slots:
};
#endif // SENDER_H@
sender.cpp
@#include "sender.h"Sender::Sender(QObject *parent) :
QObject(parent)
{
}void Sender::fireSignal()
{
QString mesg ="Sending a message.";
emit foo(mesg);
}@receiver.h
@#ifndef RECEIVER_H
#define RECEIVER_H#include <QObject>
class Receiver : public QObject
{
Q_OBJECTpublic:
explicit Receiver(QObject *parent = 0);signals:
public slots:
void bar(QString);
void wee();
};#endif // RECEIVER_H@
receiver.cpp
@#include "receiver.h"#include <QDebug>
Receiver::Receiver(QObject *parent) :
QObject(parent)
{
}void Receiver::bar(QString mesg)
{
qDebug() << mesg;
}void Receiver::wee()
{
qDebug() << "Weeee!";
}@ -
The sender and receiver objects in the constructor of your MainWindow aren't pointers, they will be deleted at the end of the constructor - hence, they can't do anything with a signal sent to them.
I didn't read all the code, but that's what you should try to change first. -
Wow, thanks for a quick response. Tried, and it worked! :)
Changed in mainwindow.cpp to:
@ Sender *sender = new Sender();
Receiver *receiver = new Receiver();QObject::connect(sender, SIGNAL(foo(QString)), receiver, SLOT(bar(QString))); QObject::connect(sender, SIGNAL(foo(QString)), ui->label, SLOT(setText(QString))); QObject::connect(ui->pushButton, SIGNAL(clicked()), ui->label, SLOT(close())); QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(getSig())); QObject::connect(ui->pushButton, SIGNAL(clicked()), receiver, SLOT(wee()));@
-
place [SOLVED] in front of your first post. Will safe us from reading solved posts!
-
Thanks. Done :)
-
OMG, I just spent good 3 hours hunting for the same stupid mistake - THANK YOU thEClaw (and thank you suiato for doing the same thing I did :-) )