You can use std::bind
This is a functional object adapter that allows functional objects to be adaptee to a given number of parameters.
For example, you create your own chat server. That contains two classes: ChatServer and ServerWorker.
ChatServer is QTcpServer class and ServerWorker is QTcpSocket ( manage the socket on the server side).
Signals in ServerWorker header:
void error();
In your ChatServer header you define these private slots:
void userError(ServerWorker *sender);
In cpp file you create these object and in incomingConnection
method that run after socket connect, you connect slots and signals using std::bind
:
void ChatServer::incomingConnection(qintptr socketDescriptor)
{
//some code
connect(worker, &ServerWorker::error, this, std::bind(&ChatServer::userError, this, worker));
}
std::bind
creates a functor with some fixed arguments. For example connect(worker, &ServerWorker::error, this, std::bind(&ChatServer::userError, this, worker));
will result in this->userError(worker)
; to be called every time the worker emits the error signal.
userErrorslot
is executed every time a socket connected to a client encounters an error. It has signature:
void ChatServer::userError(ServerWorker *sender)
{
//some code
}
Example