1
votes

I'm having little problem here. I'm having troubles with signals & slots. I'll try to explain with pseudocode. So, here it goes.

I have main thread (mainwindow.cpp) where I'm creating new objects when new client connects.

mainwindow.h:

signals:
   void changeText();
...

mainwindow.cpp:

connect(tcpserver, SIGNAL(changeText()), this, SIGNAL(changeText()));

...

MyClass *m = new MyClass(this);
connect(this, SIGNAL(changeText()), m, SLOT(changeText()));

I have also tcpserver class, which creates new QThread when client connects and connects both signals.

tcpserver.h:

signals:
    void changeText();
protected:
    void incomingConnection(int handle);

tcpserver.cpp:

void incomingConnection(int handle)
{
    QTcpSocket *s = new QTcpSocket(this);
    s->setSocketDescriptor(handle);
    mythread *thread = new mythread(s, this);
    connect(thread, SIGNAL(changeText()), this, SIGNAL(changeText()));
}

The problem is following: I have 3 objects, main thread, tcpserver class which inherits from QTcpServer, and the mythread class which inherits from QThread class.

When I'm creating new "mythread"-s from "tcpserver" and connecting signals, all signals from new threads are connected to "tcpserver"-s signals, so, e.g if 10 clients do something, all signals are sent to "tcpserver" which makes problem for me. I want to understand which signal should I connect to MyClass via QTread -> TcpServer -> MainWindow.

Sorry for my English, if you don't understand what I mean, I'll try to explain better.

Thank you.

1
I don't understand what you mean by "makes problem for me" are you trying to differentiate the incoming connections? If so you can use the int socketDescriptor as a signal argument to differentiate the calls in the MyClass slot - Viv
I'm bit confused here, because I'm new to Qt. If I pass socketDescriptor in signal, it will still cause a lot of signals to be emitted instead of one-by-one. Can you show me little example of it please? - Nika
I'm sorry but I do not understand your problem. I do not mind showing you an example provided I understand what you are trying to do. Can you update your question and state what your expected output should be and what your currently getting. - Viv
You have not stated what you want or why the current situation is a problem for you. Without either information there is no way to help. - Thomas
I'm sorry, code is really big but I can show you graph of flow how it should work. s21.postimage.org/yfm1ufudz/Signal_Slot.png If you take a look here, I want to QThread_1's signal to be connected to Object_1, QThread_2's to Object_2 and so on.. - Nika

1 Answers

0
votes

Some suggestions:

  • in your "incomingConnection(int)", i doubt any memory leak for the objects created in heap (tcpsocket, and mythread), use QList (or other container) in your tcpserver to hold the sockets created in your "incomingConnection(int)"
  • prefix the name of SLOT(changeText) with your classes to clarify which objects is your intented sender/receiver
  • I dont think you cant connect two signal like

    connect(thread, SIGNAL(changeText()), this, SIGNAL(changeText()));