[SOLVED]Yet another Signals and Slots issue



  • Hi Im new to QT and I have a test program to test out Slots and dont know why it dose not work, it should
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "speak.h"
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    Speak Ask;
    
    //this works on startup
    Ask.Speach();
    
    //this works as it should
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(testSlotLocal()));
    //This dose not work, Why dose it not work?
    connect(ui->pushButton,SIGNAL(released()),&Ask,SLOT(Speach()));
    

    }

    void MainWindow::testSlotLocal( )
    {

    qDebug()<<"Hi there";
    

    }@

    This is the mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "speak.h"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    // ~MainWindow();

    public slots:

    // void testSlotLocal( );
    

    private slots:
    void testSlotLocal( );

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    This is my Object speak.h
    @#ifndef SPEAK_H
    #define SPEAK_H

    #include <QObject>

    class Speak : public QObject
    {
    Q_OBJECT
    public:
    explicit Speak(QObject *parent = 0);

    signals:

    public slots:
    void Speach();
    private slots:

    };

    #endif // SPEAK_H@

    and this is my speak.cpp

    @#include "speak.h"
    #include <QDebug>

    Speak::Speak(QObject *parent) :
    QObject(parent)
    {
    qDebug()<<"How Can I Help You?";
    }

    void Speak::Speach()
    {
    qDebug()<<"How Can I Help You?";
    }
    @

    So my question is why dose the slot Speach() not get executed with this connection? I just wont to press the pushButton
    and have the message called to qDebug()..

    @ connect(ui->pushButton,SIGNAL(released()),&Ask,SLOT(Speach()));@



  • Hi!
    Your Ask object is a local object for MainWindow constructor, and it is not exist outside constructor.
    Try this:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "speak.h"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    // ~MainWindow();

    public slots:

    // void testSlotLocal( );
    

    private slots:
    void testSlotLocal( );

    private:
    Ui::MainWindow *ui;
    Speak *Ask;
    };

    #endif // MAINWINDOW_H@

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    //#include "speak.h" - not needed here
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    Ask = new Speak(this);
    
    //this works on startup
    Ask->Speach();
    
    //this works as it should
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(testSlotLocal()));
    //This dose not work, Why dose it not work?
    connect(ui->pushButton,SIGNAL(released()),Ask,SLOT(Speach()));
    

    }

    void MainWindow::testSlotLocal( )
    {

    qDebug()<<"Hi there";
    

    }@



  • By the way, Rage44 welcome to DevNet!



  • Wow I can feel the love already lol
    Thanks for clearing that up it works as it should


  • Moderators

    Please mark it as solved by editing your initial post and adding "[Solved]" to the title.

    Cheers and also a cordial welcome to DevNet.



  • Hi,
    Place [SOLVED] in front of you post! That will void other people from reading this solved issue.
    Greetz and welcome!!


  • Moderators

    Hah, I just knew that would happen ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.