Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Yet another Signals and Slots issue
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Yet another Signals and Slots issue

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rage44
    wrote on last edited by
    #1

    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()));@

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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";
      

      }@

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        By the way, Rage44 welcome to DevNet!

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rage44
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

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

            Cheers and also a cordial welcome to DevNet.

            (Z(:^

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

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

              Greetz, Jeroen

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Hah, I just knew that would happen ;)

                (Z(:^

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved