Results 1 to 8 of 8

Thread: Calling functions from other classes from QThread object

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.