Results 1 to 8 of 8

Thread: Calling functions from other classes from QThread object

  1. #1
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Calling functions from other classes from QThread object

    I'm coding a small front end GUI for some chat client code I've written in C. I'm not really worrying about the networking aspect yet, just getting the GUI and signals set up etc.

    Here's what the app looks like, it's pretty simple, type input, click submit, it (will eventually) sends the message to the chat server.



    I've tried to set up a new qthread object to deal with the task of continually checking for messages sent by the chat server. The problem is when it finds one I want it to update the main "display" with the received message. I've written code to that effect but it doesn't work.

    Here's the relevent code segments:

    This is the main "display" window..
    Qt Code:
    1. class Output : public QWidget
    2. {
    3. public:
    4. Output(QWidget *parent = 0);
    5.  
    6. public slots:
    7. void displayMsg(QString msg);
    8.  
    9. private:
    10. QTextEdit *display;
    11.  
    12. };
    To copy to clipboard, switch view to plain text mode 

    And the qthread object...
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. protected:
    4. void run();
    5. };
    6.  
    7. void MyThread::run()
    8. {
    9.  
    10.  
    11. for (int count = 0; count < 20; count++)
    12. {
    13. sleep(1);
    14. Output::displayMsg("test");
    15. }
    16.  
    17.  
    18. /*char *rawmessage = (char *)malloc(MAX_MSG_LENGTH);
    19.  
    20.   while(1)
    21.   {
    22.   //rawmessage = network_getmessage();
    23.   // translate from char * to QString
    24.   //displayMsg(msg);
    25.   }
    26.   */
    27. }
    To copy to clipboard, switch view to plain text mode 
    You can see I've commented out the network stuff, I just want to get the threads communicating in a basic way before I move on.

    I get the compile time error:
    Qt Code:
    1. main.cpp: In member function 'virtual void MyThread::run()':
    2. main.cpp:94: error: cannot call member function 'void Output::displayMsg(QString)' without object
    To copy to clipboard, switch view to plain text mode 

    I can post the full source if it will help, but I think the parts above are all that is needed?

    tl;dr version: how can I call another class's member functions from within a qthread object?

    Thanks, Benjamin
    Last edited by caesius; 12th January 2010 at 23:15.

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

    Default Re: Calling functions from other classes from QThread object

    You are calling the method as a static method but it is not a static method. If you had two "Output" objects, how would "Output::displayMsg()" know which one to output the message to?

    Emit a signal from the thread every time a new message arrives and connect that signal to your displayMsg() slot. Or better yet implement your chat backend a bit differently and don't use a thread at all, you don't need it.
    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.


  3. #3
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling functions from other classes from QThread object

    Thanks wysota, I understand now. Elementry mistake.

    Could you elaborate on why I don't need a thread? I thought about this for a while and I can't see any other options...

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

    Default Re: Calling functions from other classes from QThread object

    Let's reverse the situation. Tell me why you think you need a thread. And I want you to think about details - an answer that "I want to do two things at the same time" is not a correct one.
    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.


  5. #5
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling functions from other classes from QThread object

    Alright, so I understand that the program can get away with a millisecond delay while receiving the message so I don't really need a new thread. I've tried to rewrite it accordingly but I keep running into the problem that I need to have the following somewhere in my code:
    Qt Code:
    1. for(;;)
    2. {
    3. rawmessage = network_getmessage(socket);
    4. emit recvdMsg(rawmessage);
    5. }
    To copy to clipboard, switch view to plain text mode 
    I've put it in one of the constructors but as I expected it just hangs...

    Here's my new, more condensed program (network code omitted):

    dialogue.cpp (message display, text input etc)
    Qt Code:
    1. #include "dialogue.h"
    2.  
    3. Dialogue::Dialogue(int socket)
    4. {
    5.  
    6. char *rawmessage = (char *)malloc(MAX_MSG_LENGTH);
    7.  
    8. commSocket = socket;
    9.  
    10. input = new QLineEdit(this);
    11. output = new QTextEdit(this);
    12.  
    13. QPushButton *submit = new QPushButton("Submit", this);
    14. QVBoxLayout *layout = new QVBoxLayout;
    15. layout->addWidget(input);
    16. layout->addWidget(output);
    17. layout->addWidget(submit);
    18. setLayout(layout);
    19.  
    20. connect(submit, SIGNAL(clicked()), this, SLOT(sendMsg()));
    21. connect(this, SIGNAL(recvdMsg(char *)), this, SLOT(displayMsg(char *)));
    22.  
    23.  
    24.  
    25. for(;;)
    26. {
    27. rawmessage = network_getmessage(socket);
    28. emit recvdMsg(rawmessage);
    29. }
    30.  
    31. }
    32.  
    33. void Dialogue::sendMsg(int socket)
    34. {
    35. QString qMsg = input->text();
    36. QByteArray ba = qMsg.toLatin1();
    37. char *msg = ba.data();
    38. network_sendmessage(socket, msg);
    39. }
    40.  
    41. void Dialogue::displayMsg(char *msg)
    42. {
    43. QString qMsg = msg;
    44. output->append(msg);
    45. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3.  
    4. #include "dialogue.h"
    5. #include "network.h"
    6.  
    7. class Client : public QWidget
    8. {
    9. public:
    10. Client(QWidget *parent = 0);
    11. };
    12.  
    13. Client::Client(QWidget *parent) : QWidget(parent)
    14. {
    15. int socket = network_tcpconnect();
    16.  
    17. QPushButton *quit = new QPushButton("Quit", this);
    18. Dialogue *dialogue = new Dialogue(socket);
    19.  
    20. QVBoxLayout *layout = new QVBoxLayout;
    21. layout->addWidget(quit);
    22. layout->addWidget(dialogue);
    23. setLayout(layout);
    24. }
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication app(argc, argv);
    29. Client client;
    30. client.show();
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Calling functions from other classes from QThread object

    Quote Originally Posted by caesius View Post
    (...)I need to have the following somewhere in my code:
    Qt Code:
    1. for(;;)
    2. {
    3. rawmessage = network_getmessage(socket);
    4. emit recvdMsg(rawmessage);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Let's focus on that...

    All problems and ideas related to threads boil down to this piece of code. You have an infinite loop here where you read messages from network. The network_getmessage call is most likely a blocking one. If you made it non-blocking, all your problems would go away and there would be no need to use threads. If "socket" is a BSD socket then you can use select() or QSocketNotifier (which may be using select() internally) to only read from the socket when there is actually something to be read.
    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.


  7. #7
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling functions from other classes from QThread object

    Thanks wysota, your explanations are useful.

    I must be missing something - because the one thing I still don't understand is how am I meant to check for messages OVER and OVER and OVER again?

    Whatever thought process I use it comes back to an infinite loop, i.e., if I wrote
    Qt Code:
    1. connect(this, SIGNAL(gotMsg()), dialogue, SLOT(displayMsg()));
    To copy to clipboard, switch view to plain text mode 
    then somewhere in my code I'm still going to need an infinite for loop constantly waiting for messages and sending the gotMsg() signal when one arrives (that's what I used the thread for..)

    I think I'm missing something big...

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

    Default Re: Calling functions from other classes from QThread object

    With Qt it's almost always about signals and slots Take a look at QSocketNotifier::activated().
    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.


Similar Threads

  1. Calling COM object from QT
    By mrityunjay in forum Qt Programming
    Replies: 0
    Last Post: 27th October 2009, 05:25
  2. Calling XPCOM Object from QT
    By sujith in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 10:10
  3. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05
  4. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  5. Calling QThread::exec() repeatedly
    By hb in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2007, 20:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.