Solved How to use a SIGNAL that has QVector<QTcpSocket *> as it's argument in connect()?
-
Hello,
I work on a server-client project. In the server side I make the list of current clients with
QVector<QTcpSocket *> mClients.
mClients.push_back(recievedSocket);I want to send the mClients to the clients (threads). So, I decided to make a signal and connect it to the proper slot. However, I face with this error:
"QObject::connect: No such signal serverSocketClass::listOfClients(mClients)"
although I have declared the signal.
Should I use QMetaType Class to register the QVector<QTcpSocket *>?
-
Hi
Did you add the QOBJECT macro?
It sounds more like something else than lack of registering the type. -
Yes, I have added the Q_OBJECT and I made another signal without any argument and it worked. So, It looks the problem is because of the SIGNAL argument which is a Vector<QTcpSocket *>. I tried to register it with qRegisterMetaType<QVector<QTcpSocket *>>(); but it does not work. Maybe I made some mistakes in case of registration.
-
@Bamshad
Hi
In which way. "Dont work" ?
Normally it would complain if type needs registering.I did
class MainWindow : public QMainWindow { Q_OBJECT private slots: void GetIT( QVector<QTcpSocket *> ); signals: void sendIT( QVector<QTcpSocket *> );
connect( this, &MainWindow::sendIT, this, &MainWindow::GetIT );
QVector<QTcpSocket *> test;
emit sendIT(test);and it just compiled.
Did you give it the variable name and not the type in declaration something like that?
-
It is a server-client project and the signal emits after first client is arrived. In this situation, is it considered as a "queued signal"? Because, in the documentation of QMetaType it is mentioned that:
"The class is used as a helper to marshall types in QVariant and in queued signals and slots connections".
That is why I think maybe I need to use QMetaType class. However, I should mention that other signals that are related to the sockets like clientDisconnected() (which also will be used later) work fine. So, I am so confused. I do not now if it is a queued signal.
-
@Bamshad
Hi
The connect type is Auto pr default.
So should not be that.
Also here it just compiled and worked so i think we are looking at something else.
Could you try to add the signal to the serverSocketClass again and then
completely delete the build folder and rebuild all ? -
connect( this, &MainWindow::sendIT, this, &MainWindow::GetIT );
In this line, have you passed the argument to the signal?
in my case it is like this:
connect(this,SIGNAL(listOfClients(mClients)),this,SLOT(setListofClients(QVector<QTcpSocket *>)));
Is it correct syntax?
-
Hi,
No that syntax is wrong.
connect
only connects two methods. You can't pass parameters at that time. It's when you want the to emit the signal that you pass the value needed. -
Hi
mClients is only used in the emit.
like
connect( this, &MainWindow::sendIT, this, &MainWindow::GetIT );
QVector<QTcpSocket *> mClients;
emit sendIT(mClients); -
@mrjj and @SGaist Thank you very much.
Yes, it was my syntax mistake. So I changed
connect(this,SIGNAL(listOfClients(mClients)),this,SLOT(setListofClients(QVector<QTcpSocket *>)));
To
connect(this,&serverSocketClass::listOfClients,thread,&clientSocketClass::setListofClients);
emit listOfClients(mClients);
and now it works!!
I wanted to read QMetaType Class that was not related to the problem.