Signal and Slot connection not working
-
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(); } }
-
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. -
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(); } }
-
@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] -
@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? -
@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] -
@JoeCFD The QObject::connect(&_RecordData[_NewRecordId],... run before
_NewRecordId = QDateTime::currentDateTime().toString("yyyy.MM.dd-hh.mm.ss.zzz"); ?When and how are things being added to
_RecordData
? You can't have the key be a datetime string derived from a call to theQDateTime::currentDateTime()
and then try to retrieve a value from the map later on by callingQDateTime::currentDateTime()
a subsequent time to use as the search key. How would a value exist in it with a key of the current DateTime? -
When and how are things being added to
_RecordData
? You can't have the key be a datetime string derived from a call to theQDateTime::currentDateTime()
and then try to retrieve a value from the map later on by callingQDateTime::currentDateTime()
a subsequent time to use as the search key. How would a value exist in it with a key of the current DateTime?@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 callingQDateTime::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? -
-
const bool connected = QObject::connect(&_RecordData[_NewRecordId], &DataStream::onDataReceived, this, &Saving::onRecordingData, Qt::UniqueConnection); qDebug() << "Connection established?" << connected;
Return true;
@isan
OK, fair enough. So theconnect()
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 theQDateTime::currentDateTime()
and then try to retrieve a value from the map later on by callingQDateTime::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.