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 connection not working
Qt 6.11 is out! See what's new in the release blog

Signal and Slot connection not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.1k Views 2 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by
    #1

    When I press the pushButton to save data, on_Recording () is called, but in this function the signal and slot connection does not work. Why?

    //define in Saving.h
    std::unordered_map<QString,DataStream>          _RecordData;
    std::unordered_map<QString,ServerStream>        _RecordProcessor;
    
    
    void Saving::on_Recording()
    {
      _NewRecordId =  QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");
    
      QObject::connect(&_RecordData[_NewRecordId], &DataStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
      QObject::connect(&_RecordProcessor[_NewRecordId], &ServerStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
    }
    
    
    void Saving::onRecordingData(QByteArray data) {
      QFile _File = QFile(dir.filePath(_NewRecordId + ".txt"));
      if (_File.open(QIODevice::WriteOnly | QIODevice::Append)) {
        _File.write(data);
        _File.flush();
        _File.close();
      }
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly are you expecting to happen ?
      If you think the connection is failing, you should check the return value of your connect calls.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • I isan

        When I press the pushButton to save data, on_Recording () is called, but in this function the signal and slot connection does not work. Why?

        //define in Saving.h
        std::unordered_map<QString,DataStream>          _RecordData;
        std::unordered_map<QString,ServerStream>        _RecordProcessor;
        
        
        void Saving::on_Recording()
        {
          _NewRecordId =  QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");
        
          QObject::connect(&_RecordData[_NewRecordId], &DataStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
          QObject::connect(&_RecordProcessor[_NewRecordId], &ServerStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
        }
        
        
        void Saving::onRecordingData(QByteArray data) {
          QFile _File = QFile(dir.filePath(_NewRecordId + ".txt"));
          if (_File.open(QIODevice::WriteOnly | QIODevice::Append)) {
            _File.write(data);
            _File.flush();
            _File.close();
          }
        }
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @isan
        _RecordData[_NewRecordId]

        What do you think this is? _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz"); is a unique new string, so what's going on?

        I 1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And to add to @JonB how did you declare your classes ?
          QObject based classes cannot be copied, so there's also likely something fishy going on there.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @isan said in Signal and Slot connection not working:

            _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");

            QObject::connect(&_RecordData[_NewRecordId]

            JonB is right.
            _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");
            brand new time. _RecordData[_NewRecordId] does not exist yet and can not be used to connect.
            QObject::connect(&_RecordData[_NewRecordId]

            I 1 Reply Last reply
            0
            • JonBJ JonB

              @isan
              _RecordData[_NewRecordId]

              What do you think this is? _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz"); is a unique new string, so what's going on?

              I Offline
              I Offline
              isan
              wrote on last edited by isan
              #6

              @JonB For handle multiple record I define like this

              JonBJ 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @isan said in Signal and Slot connection not working:

                _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");

                QObject::connect(&_RecordData[_NewRecordId]

                JonB is right.
                _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz");
                brand new time. _RecordData[_NewRecordId] does not exist yet and can not be used to connect.
                QObject::connect(&_RecordData[_NewRecordId]

                I Offline
                I Offline
                isan
                wrote on last edited by
                #7

                @JoeCFD The QObject::connect(&_RecordData[_NewRecordId],... run before
                _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz"); ?

                M 1 Reply Last reply
                0
                • I isan

                  @JonB For handle multiple record I define like this

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

                  @isan
                  As @SGaist said earlier, the connect()s return a value to indicate whether the connection was made successfully. Print that out/test it.

                  I 1 Reply Last reply
                  0
                  • I isan

                    @JoeCFD The QObject::connect(&_RecordData[_NewRecordId],... run before
                    _NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz"); ?

                    M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #9

                    When and how are things being added to _RecordData? You can't have the key be a datetime string derived from a call to the QDateTime::currentDateTime() and then try to retrieve a value from the map later on by calling QDateTime::currentDateTime() a subsequent time to use as the search key. How would a value exist in it with a key of the current DateTime?

                    I 1 Reply Last reply
                    3
                    • M mchinand

                      When and how are things being added to _RecordData? You can't have the key be a datetime string derived from a call to the QDateTime::currentDateTime() and then try to retrieve a value from the map later on by calling QDateTime::currentDateTime() a subsequent time to use as the search key. How would a value exist in it with a key of the current DateTime?

                      I Offline
                      I Offline
                      isan
                      wrote on last edited by
                      #10

                      @mchinand said in Signal and Slot connection not working:

                      You can't have the key be a datetime string derived from a call to the QDateTime::currentDateTime() and then try to retrieve a value from the map later on by calling QDateTime::currentDateTime() a subsequent time to use as the search key.

                      How can I fix it? Can I convert QDateTime::currentDateTime() to something else then use it for map?

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @isan
                        As @SGaist said earlier, the connect()s return a value to indicate whether the connection was made successfully. Print that out/test it.

                        I Offline
                        I Offline
                        isan
                        wrote on last edited by isan
                        #11

                        @JonB

                        const bool connected =   QObject::connect(&_RecordData[_NewRecordId], &DataStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
                         qDebug() << "Connection established?" << connected;
                        

                        Return true;

                        JonBJ 1 Reply Last reply
                        0
                        • I isan

                          @JonB

                          const bool connected =   QObject::connect(&_RecordData[_NewRecordId], &DataStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection);
                           qDebug() << "Connection established?" << connected;
                          

                          Return true;

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

                          @isan
                          OK, fair enough. So the connect() works, but I don't understand how you will actually have any useful object at _RecordData[_NewRecordId] which you will actually use later to raise the signal?

                          Read what @mchinand says above, which is what I have been saying from the start:

                          When and how are things being added to _RecordData? You can't have the key be a datetime string derived from a call to the QDateTime::currentDateTime() and then try to retrieve a value from the map later on by calling QDateTime::currentDateTime() a subsequent time to use as the search key. How would a value exist in it with a key of the current DateTime?

                          I don't know how to express it any clearer than that.

                          1 Reply Last reply
                          1

                          • Login

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