[SOLVED] Problem with Signals in QThread.



  • Why Qt forum is blocking my post ?



  • Wtf, When I include my code it's considered like a spam...



  • Hello,

    I need your help.
    Actually, I'm codding a multithreaded server but, I'm encounter an issue and I don't know why.
    Perhaps you can help me.

    My_MainWindow_Class:

    @void MainWindow::incomingConnection()
    {
    int socketDescriptor;
    string connected_nbr = std::to_string(g_conected_nbr + 1);

    g_conected_nbr += 1;
    socketDescriptor = 0;
    cout << "Connecting : " << socketDescriptor << endl;
    ui->textBrowser_2->setText("Connected (" + QString(connected_nbr.c_str()) + " client)");
    my_soc = my_srv.nextPendingConnection();
    MyThread *new_thread = new MyThread(my_soc->socketDescriptor());
    connect(new_thread, SIGNAL(client_disconnected()), this, SLOT(delete_client()));
    connect(new_thread, SIGNAL(finished()), new_thread, SLOT(deleteLater()));
    new_thread->start();
    

    }@



  • My_Kill Thread method

    @void MyThread::kill_thread()
    {
    emit client_disconnected();
    std::cout << "Disconnected !" << std::endl;
    my_soc->deleteLater();
    }@

    My_Thread Run Method:

    @
    void MyThread::run()
    {
    std::cout << "Socket n"<< socketDesciptor << ": Starting..." << std::endl;
    my_soc = new QTcpSocket();
    if (!my_soc->setSocketDescriptor(this->socketDesciptor))
    {
    return;
    }
    connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()));
    connect(my_soc, SIGNAL(disconnected()), this, SLOT(kill_thread()));
    std::cout << "Client Connected !" << std::endl;
    exec();
    }@



  • The problem in this code:

    The signal client_disconnected() is never emitted by the thread to my MainWindow class.
    Consequence:
    @connect(new_thread, SIGNAL(client_disconnected()), this, SLOT(delete_client()));@

    Do nothing.

    Please help me, I don't now why this signal isn't emited :/ because in my Thread connections work fine !

    Thanks a lot.



  • Here is My_Thread.h code:

    @class MyThread : public QThread
    {
    Q_OBJECT

    public:
    explicit MyThread(int ID);
    ~MyThread();
    void run();

    signals:
    void client_disconnected();

    public slots:
    void kill_thread();
    void tcp_receve_commands();

    private:
    QTcpSocket *my_soc;
    int socketDesciptor;
    QString old_question;

    };@



  • Here is includes above My_Thread.h code (because forum block my post when i add them)

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    @

    Sorry for multiples posts but Qt forum consider my entire post like a spam.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure your socked is getting deconnected properly ?

    You can also check your implementation against the "Threaded Fortune Server" example


  • Moderators

    Hi,

    [quote author="andnerb" date="1422204990"]
    @
    connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()));
    @
    [/quote]tcp_receive_commands() (and all of MyThread's slots) will end up running in the main thread. I presume you don't want this.

    The "QThread documentation":http://doc.qt.io/qt-5/qthread.html says (emphasis added):

    • "It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread."

    If you need signals and slots to communicate with your thread, don't subclass QThread. Use a worker object instead (see the QThread documnetation for an example)

    [quote author="SGaist" date="1422223035"]You can also check your implementation against the "Threaded Fortune Server" example[/quote]The Threaded Fortune Server (TFS) is designed very differently from andnerb's code. TFS runs a "single-shot" thread every time a new connection comes in, and the thread quits immediately after sending one message. There is no event loop or custom slots.


  • Lifetime Qt Champion

    Indeed both are different, I was just suggesting to compare both implementation in the case he was looking for a workflow similar to the TFS but missed the exemple.



  • Ok, thanks a lot for your answers !
    I will see QThread documentation, with worker class.



  • I've an other Question:

    After create my_worker and try to move it in my QThread I got this error during execution:

    @QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNativeSocketEngine(0x15d345b8), parent's thread is QThread(0x15cfdc78), current thread is QThread(0x15d345d8).@

    And worker stop to work...

    I don't know why ?
    Is it problem with my QTcpServer ?

    I haven't any exec method, because Worker herit from QObject and My Worker stop to work after one request attempt to my server.

    EDIT :

    Problem solved:

    I'm using :

    @connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()), Qt::DirectConnection);
    connect(my_soc, SIGNAL(disconnected()), this, SLOT(kill_thread()), Qt::DirectConnection);@

    Now, because without Qt::DirectConnection, connections aren't executed in the Thread.

    Thanks a lot all for your help, my problem is solved !
    I love Qt ^^ !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.