Results 1 to 8 of 8

Thread: connec signal slot in thread problem

  1. #1
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default connec signal slot in thread problem

    Hi i got slot not found that i already declare

    class MyReadThread ublic QThread{

    public:
    QTcpSocket *ms;
    MyReadThread(QTcpSocket*s){
    ms = s;
    connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
    }
    void run(){
    qDebug() << "mythread run";

    }
    private slots:

    void on_socket_readyRead(void){
    qDebug() << ms->readAll();
    }

    };
    void qtnetwork:n_server_newConnection(void)
    {
    qDebug() << "new connection";
    QTcpSocket *s = sv->nextPendingConnection();

    MyReadThread *t = new MyReadThread(s);
    t->start();


    }
    result:

    QObject::connect: No such slot QThread:n_socket_readyRead(void) in qtnetwork.cpp:44
    connected
    mythread run
    The thread 0x23a8 has exited with code 0 (0x0).

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connec signal slot in thread problem

    First MyReadThread class declaration must contain Directive Q_OBJECT.
    Second without executing event loop thread will not receive signals from other threads. Method run should looks like :
    Qt Code:
    1. void run(){
    2. qDebug() << "mythread run";
    3. QThread::run();
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connec signal slot in thread problem

    "t" object lives in the main thread therefore the slot will be executed in the main thread and not in the thread represented by "t". The rest of the code does not make sense either, the socket also lives in the main thread so overall the thread does exactly nothing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: connec signal slot in thread problem

    after i add Q_OBJECT to class i get new link error message

    1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyReadThread::metaObject(void)const " (?metaObject@MyReadThread@@UBEPBUQMetaObject@@XZ)
    1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyReadThread::qt_metacast(char const *)" (?qt_metacast@MyReadThread@@UAEPAXPBD@Z)
    1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyReadThread::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyReadThread@@UAEHW4Call@QMetaObject @@HPAPAX@Z)
    1>C:\Users\Jack\documents\visual studio 2013\Projects\qtnetwork\Win32\Debug\\qtnetwork.exe : fatal error LNK1120: 3 unresolved externals
    i will improve logic later ,now need to solve problem why can't connect signal/slot.


    Every Qt generate file are added


    I might give up Qt and forgot programming Lol ,my brain can't handle Qt framework so complicate ?

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: connec signal slot in thread problem

    Just a guess but try running qmake, then try to rebuild. If that doesn't work, post your current version of code using the [code] and [/code] tags so that your code is properly formatted.

  6. #6
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: connec signal slot in thread problem

    Qt Code:
    1. #include "qtnetwork.h"
    2. #include <QtNetwork\qtcpsocket.h>
    3. #include <QtNetwork\qtcpserver.h>
    4. #include <QtCore\qthread.h>
    5. qtnetwork::qtnetwork(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. ui.setupUi(this);
    9.  
    10. //connect(ui.actionTest, SIGNAL(triggered(bool)), this, SLOT(on_actionTest_triggered(bool)));
    11.  
    12. }
    13.  
    14. qtnetwork::~qtnetwork()
    15. {
    16.  
    17. }
    18.  
    19. void qtnetwork::on_actionTest_triggered(bool)
    20. {
    21. qDebug() << "hitme";
    22.  
    23.  
    24.  
    25.  
    26. sv = new QTcpServer(this);
    27. sv->listen(QHostAddress::LocalHost, 80);
    28. connect(sv, SIGNAL(newConnection(void)), this, SLOT(on_server_newConnection(void)));
    29.  
    30. s = new QTcpSocket(this);
    31. s->connectToHost("localhost", 80);
    32. connect(s, SIGNAL(connected()), this, SLOT(on_socket_connect(void)));
    33. connect(s, SIGNAL(readyRead()), this, SLOT(on_socket_readyread(void)));
    34. //delete s;
    35.  
    36.  
    37. }
    38. class MyReadThread :public QThread{
    39. Q_OBJECT
    40. public:
    41. MyReadThread(QTcpSocket*s){
    42. ms = s;
    43. connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
    44. }
    45. void run(){
    46. qDebug() << "mythread run";
    47. QThread::run();
    48. }
    49. private slots:
    50.  
    51. void on_socket_readyRead(void){
    52. qDebug() << ms->readAll();
    53. }
    54.  
    55. };
    56. void qtnetwork::on_server_newConnection(void)
    57. {
    58. qDebug() << "new connection";
    59. QTcpSocket *s = sv->nextPendingConnection();
    60.  
    61. MyReadThread *t = new MyReadThread(s);
    62. t->start();
    63.  
    64.  
    65. }
    66. void qtnetwork::on_socket_connect(void)
    67. {
    68. qDebug() << "connected";
    69. const char buffer[] = { "Hello's world" };
    70. int len = sizeof(buffer);
    71. s->write(buffer, len);
    72. }
    73. void qtnetwork::on_socket_readyread(void)
    74. {
    75. qDebug() << s->readLine();
    76. QByteArray t = s->readAll();
    77. ui.plainTextEdit->appendPlainText("server:"+t);
    78. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connec signal slot in thread problem

    You are still not using the thread for anything. All your objects live in the main thread. At line 78 you would get a crash if the slot executed in a worker thread. I really suggest you remove the thread completely (just replace QThread with QObject).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: connec signal slot in thread problem

    Quote Originally Posted by Teerayoot View Post
    after i add Q_OBJECT to class i get new link error message
    As jthomps already said that can be fixed by re-running qmake.
    This is only necessary because you added the Q_OBJECT marker after the header had been added to the project.

    You can easily avoid this by always putting that marker in class declarations of QObject derived classes.

    I also agree with wysota that you are making your life unnecessarily complicated by attempting too many things at once.
    Just concentrate on the networking bits and keep threading for a later stage, if you need it at all.

    Cheers,
    _

Similar Threads

  1. Thread signal/slot problem
    By K4ELO in forum Qt Programming
    Replies: 4
    Last Post: 1st January 2013, 13:34
  2. Qt Jambi: Thread and signal slot problem
    By newb_developer in forum Qt-based Software
    Replies: 1
    Last Post: 18th December 2012, 23:06
  3. Quit thread using signal/slot
    By mounte in forum Qt Programming
    Replies: 5
    Last Post: 31st January 2011, 20:14
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. Replies: 16
    Last Post: 28th October 2008, 22:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.