Results 1 to 2 of 2

Thread: Qt Jambi: Thread and signal slot problem

  1. #1
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt Jambi: Thread and signal slot problem

    Hello,
    I develop application in Java using Qt Jambi 4.7.1.

    I created the following GUI class (it seeems to be ok):
    Qt Code:
    1. package org.margor.echoclient;
    2.  
    3. import com.trolltech.qt.gui.*;
    4.  
    5. public class MainWindow extends QMainWindow {
    6. /*
    7. * Objects / controls.
    8. */
    9. Ui_MainWindow mainw = new Ui_MainWindow();
    10. Client client = new Client();
    11.  
    12. /*
    13. * Methods.
    14. */
    15. public MainWindow() {
    16. mainw.setupUi(this);
    17. setWindowIcon(new QIcon("classpath:com/trolltech/images/qt-logo.png"));
    18. set_signals_slots();
    19. send_address.emit("127.0.0.1");
    20. send_port.emit(8189);
    21. client.c_connect();
    22.  
    23. }
    24. protected void set_signals_slots()
    25. {
    26. // GUI settings
    27. mainw.cleanButton.clicked.connect(this, "clean_server_message_box()");
    28. // client settings
    29. client.info.connect(this, "clean_server_message_box()");
    30. client.error.connect(this, "get_error(String, String)");
    31. send_address.connect(client, "recv_address(String)");
    32. send_port.connect(client, "recv_port(Integer)");
    33. // send message from client
    34. mainw.sendButton.clicked.connect(this, "send_client_message()");
    35. send_message.connect(client, "send_input_string(String)");
    36. }
    37. /*
    38. * Signals.
    39. */
    40. // send IP address
    41. public Signal1<String> send_address = new Signal1<String>();
    42. // send server port
    43. public Signal1<Integer> send_port = new Signal1<Integer>();
    44. // send message
    45. public Signal1<String> send_message = new Signal1<String>();
    46.  
    47. /*
    48. * Slots.
    49. */
    50. protected void helloWorld() {
    51. QMessageBox.information(this, tr("Hello"), tr("Hello World"));
    52. }
    53. protected void get_information(String title, String message) {
    54. QMessageBox.information(this, tr(title), tr(message));
    55. }
    56. protected void get_error(String title, String message) {
    57. QMessageBox.critical(this, tr(title), tr(message));
    58. }
    59. // clean chat box
    60. protected void clean_server_message_box() {
    61. mainw.display_textEdit.clear();
    62. }
    63. // send client message
    64. protected void send_client_message() {
    65. send_message.emit(mainw.message_lineEdit.text());
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

    I have problem in my model class. I am unable to send signal to my GUI class.
    Qt Code:
    1. package JambiTest;
    2.  
    3. import java.util.Observable;
    4. import java.util.Observer;
    5.  
    6. import com.trolltech.qt.core.QObject;
    7.  
    8. public class Client extends QObject implements Observer {
    9. /*
    10. * Data.
    11. */
    12. protected int number;
    13. protected Thread t = null;
    14. protected CounterThread counter = null;
    15.  
    16. /*
    17. * Signals.
    18. */
    19. Signal1<Integer> send_number = new Signal1<Integer>();
    20. Signal2<String, String> send_info = new Signal2<String, String>();
    21.  
    22. Client() {
    23. counter = new CounterThread();
    24. counter.addObserver(this);
    25. number = 0;
    26. }
    27.  
    28. /*
    29. * Slots.
    30. */
    31. // runs counter thread
    32. public void start_counter() {
    33. t = new Thread(counter);
    34. send_info.emit("Info", "Thread started");
    35. t.start();
    36. }
    37.  
    38. public class CounterThread extends Observable implements Runnable {
    39. @Override
    40. public void run() {
    41. while(true) {
    42. try {
    43. Thread.sleep(10);
    44. } catch (InterruptedException e) {
    45. e.printStackTrace();
    46. }
    47. ++number;
    48. number %= 10;
    49. setChanged();
    50. notifyObservers(number);
    51. }
    52. }
    53. }
    54.  
    55. @Override
    56. public void update(Observable arg0, Object arg1)
    57. {
    58. if(arg1 instanceof Integer)
    59. send_number.emit((Integer) arg1);
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 

    I get the exception:
    Exception in thread "Thread-2" QObject used from outside its own thread, object=JambiTest::Client(0x5549a80) , objectThread=Thread[main,5,main], currentThread=Thread[Thread-2,5,main]
    at com.trolltech.qt.GeneratorUtilities.threadCheck(Un known Source)
    at com.trolltech.qt.core.QObject.signalsBlocked(Unkno wn Source)
    at com.trolltech.qt.internal.QSignalEmitterInternal$A bstractSignalInternal.emit_helper(Unknown Source)
    at com.trolltech.qt.QSignalEmitter$Signal1.emit(Unkno wn Source)
    at JambiTest.Client.update(Client.java:59)
    at java.util.Observable.notifyObservers(Unknown Source)
    at JambiTest.Client$CounterThread.run(Client.java:50)
    at java.lang.Thread.run(Unknown Source)
    The problem is caused because update method is in other thread than GUI (the same thread as run), which is quite strange for me?

    I tried to use only signal class, and rewritten model class:
    Qt Code:
    1. package JambiTest;
    2.  
    3. import com.trolltech.qt.core.QObject;
    4.  
    5. public class Client extends QObject {
    6. /*
    7. * Data.
    8. */
    9. protected int number;
    10. protected Thread t = null;
    11. protected CounterThread counter = null;
    12.  
    13. /*
    14. * Signals.
    15. */
    16. Signal1<Integer> send_number = new Signal1<Integer>();
    17. Signal2<String, String> send_info = new Signal2<String, String>();
    18.  
    19. Client() {
    20. counter = new CounterThread();
    21. number = 0;
    22. counter.send_thread_number.connect(this, "update(Integer)");
    23. }
    24.  
    25. /*
    26. * Slots.
    27. */
    28. // runs counter thread
    29. public void start_counter() {
    30. t = new Thread(counter);
    31. send_info.emit("Info", "Thread started");
    32. t.start();
    33. }
    34.  
    35. public class CounterThread extends QObject implements Runnable {
    36. public Signal1<Integer> send_thread_number = new Signal1<Integer>();
    37. @Override
    38. public void run() {
    39. while(true) {
    40. try {
    41. Thread.sleep(10);
    42. } catch (InterruptedException e) {
    43. e.printStackTrace();
    44. }
    45. ++number;
    46. number %= 10;
    47. send_thread_number.emit(number);
    48. }
    49. }
    50. }
    51.  
    52. public void update(Integer i)
    53. {
    54. send_number.emit(i);
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    However, I get exception:
    Exception in thread "Thread-2" QObject used from outside its own thread, object=JambiTest::Client$CounterThread(0x56f9b20) , objectThread=Thread[main,5,main], currentThread=Thread[Thread-2,5,main]
    at com.trolltech.qt.GeneratorUtilities.threadCheck(Un known Source)
    at com.trolltech.qt.core.QObject.signalsBlocked(Unkno wn Source)
    at com.trolltech.qt.internal.QSignalEmitterInternal$A bstractSignalInternal.emit_helper(Unknown Source)
    at com.trolltech.qt.QSignalEmitter$Signal1.emit(Unkno wn Source)
    at JambiTest.Client$CounterThread.run(Client.java:47)
    at java.lang.Thread.run(Unknown Source)
    Thanks for your attention,
    Regards,
    Last edited by newb_developer; 18th December 2012 at 22:04. Reason: Please move to 'Qt programming' forums.

  2. #2
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Jambi: Thread and signal slot problem

    Please delete thread, put wrong code at first listing. Sorry!

Similar Threads

  1. Quit thread using signal/slot
    By mounte in forum Qt Programming
    Replies: 5
    Last Post: 31st January 2011, 21:14
  2. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 09:06
  3. Replies: 9
    Last Post: 28th November 2009, 21:31
  4. Replies: 16
    Last Post: 28th October 2008, 23:00
  5. Replies: 1
    Last Post: 11th September 2008, 21:45

Tags for this Thread

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.