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. Signal and slot not working properly
Forum Updated to NodeBB v4.3 + New Features

Signal and slot not working properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 275 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.
  • U Offline
    U Offline
    UG SEP
    wrote on last edited by
    #1

    Hey,
    I have created a signal and slot that I send data from the second window to the main window
    whenever the user clicks the button
    But I not sending the signal
    Here is the signal in is in main window

     Login log;
        connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));
    

    receiveinfo function this also exist in main window

    void Mainwindow::recieveinfo(QString username)
    {
       name=username;
    }
    

    slot in mainwindow.h

    public slots:
        void recieveinfo(QString name);
    

    signal in second window.cpp

    signals:
     void sendinfo(QString name);
    

    sendinfo emit from push button

    emit sendinfo(ui->name->text());
    

    The string name doesn't change means there is a problem in signal and slots
    Please help me
    Thanks in advance

    JonBJ 1 Reply Last reply
    0
    • U UG SEP

      Hey,
      I have created a signal and slot that I send data from the second window to the main window
      whenever the user clicks the button
      But I not sending the signal
      Here is the signal in is in main window

       Login log;
          connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));
      

      receiveinfo function this also exist in main window

      void Mainwindow::recieveinfo(QString username)
      {
         name=username;
      }
      

      slot in mainwindow.h

      public slots:
          void recieveinfo(QString name);
      

      signal in second window.cpp

      signals:
       void sendinfo(QString name);
      

      sendinfo emit from push button

      emit sendinfo(ui->name->text());
      

      The string name doesn't change means there is a problem in signal and slots
      Please help me
      Thanks in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @UG-SEP
      Start with qDebug() << ui->name->text() just above the emit line and qDebug() << username as first line in Mainwindow::recieveinfo(). Does the slot actually get called?

      It is confusing that you have recieveinfo(QString name) in the header declaration but Mainwindow::recieveinfo(QString username) in the definition. Keep those parameter names the same.

      In the longer run it is better to pass const QString & parameters than QString, but that won't be the problem here.

      And finally do yourself a favor and change over to New Signal Slot Syntax, it will help you in the long run.

      1 Reply Last reply
      2
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @UG-SEP said in Signal and slot not working properly:

        Login log;
        connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));

        this seems like a lifetime problem.

        I would bet, that log is beeing destroyed sometime later, at the ‚}


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          Killed by scope +1

          void buttonclick() {
           Login log;
           connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));
          } // log will be deleted here so sendinfo will never trigger
          
          
          make  
          Login log; 
          part of the "second window" as a member, not as a local variable
          
          
          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