Ubuntu 16.04, Qt 5.6.

There is a slot, it is addressed from the streams. It is necessary to make a turn. How can this be implemented?

//action_user.cpp void Action_user::Action_route() { DB_connect connectDB; connect(this, SIGNAL(AddUser(int, QStringList)), &connectDB, SLOT(AddDB(int, QStringList))); connect(this, SIGNAL(AddCargo(int, QStringList)), &connectDB, SLOT(AddDB(int, QStringList))); switch (route_signal) { case 1: emit AddUser(this->route_signal, parametrList); break; case 2: emit AddCargo(this->route_signal, parametrList); break; default: break; } } //db_connect.cpp void DB_connect::AddDB(int route, QStringList parametrList) { switch (route) { case 1: addUser(parametrList); break; case 2: addCargo(parametrList); break; default: break; } } void addUser (QStringList parametrList) { // Запись данных в БД } 
  • Call Queue? So if a slot is called from different threads, then the signals will line up on their own automatically. - alexis031182
  • I for some reason do not line up. Flows are different. - shaman888
  • How do you determine that you do not line up? How do you create streams and how do you call a slot out of them? You need to paint all these points in the question, otherwise it turns out a game of riddles. - alexis031182
  • Well then you get that Action_route() and AddDB slot between themselves in identical threads are functioning. In this case, specify the Qt::QueuedConnection flag explicitly for connect() . - alexis031182
  • With this key, for some reason, he does not want to go into the slot - shaman888

1 answer 1

If I understand correctly what you need, then you just need to properly connect the signal slot. the connect function has one more parameter, which usually nobody touches, since by default it determines “how the signal will be processed”. By default, there is Qt::AutoConnection . If the signal slot is within the same thread, then it will be Qt::DirectConnection - roughly speaking, it is just a function call in place.

If the signal slot is in different threads, then Qt::AutoConnection is Qt::QueuedConnection - the signal is added to the event loop and the thread is needed and when the eventloop is free and the signal is received from the queue, it will be processed.

There is also Qt::BlockingQueuedConnection - it is like Qt::QueuedConnection , but only the calling thread (the one who issues the signal) is blocked until the signal is processed ( I think this is what you are looking for! ).

Example

 QObject::connect(scrollBar, SIGNAL(valueChanged(int value)), label, SLOT(setNum(int value)), Qt::BlockingQueuedConnection); 
  • writes to the console: Qt: Dead lock detected while activating a BlockingQueuedConnection: Sender is Action_user (0x7f9611981930), receiver is DB_connect (0x7f9611981820) - shaman888
  • 2
    Because you have a recipient and sent in one thread. And this case can not be processed. But logically it is equivalent to Qt::DirectConnection . - KoVadim
  • In general, I planned to collect data from threads and process the result in the main thread, this is necessary for working with the database. - shaman888