Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Signal and slot not working properly

    General and Desktop
    4
    4
    88
    Loading More Posts
    • 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
      UG SEP last edited by

      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

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @UG SEP last edited by

        @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 Reply Quote 2
        • J.Hilk
          J.Hilk Moderators last edited by

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          1 Reply Last reply Reply Quote 4
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            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 Reply Quote 0
            • First post
              Last post