[Qt-jambi-interest] Qt Jambi threading once again.

Tomasz 'Trog' Welman twelman at swmud.pl
Mon Mar 3 15:43:06 CET 2008


Hello.
I have the following (common) problem.
I have my QLabel class object created in the main thread
(class TestWidget) an a ThreadClass class representing another
thread. I've read this list, and Trolltech's docs and using
this knowledge i'd moved the QLabel class object to the
ThreadClass thread with moveToThread, but I am still having
the same exception, which is:

Exception in thread "Thread-2" QObject used from outside its own thread, object=com.trolltech.qt.gui.QLabel at 2e7820, objectThread=Thread[main,5,main], currentThread=Thread[Thread-2,5,main]
      	at com.trolltech.qt.QtJambiInternal.threadCheck(QtJambiInternal.java:509)
      	at com.trolltech.qt.core.QObject.moveToThread(QObject.java:99)
      	at test.ThreadClass.run(ThreadClass.java:19)

What am I doing wrong?
In Qt (C++) this solution works fine.



TestWidget class:


package test;
import com.trolltech.qt.gui.*;

public class TestWidget extends QWidget{

         public QLabel l;

         public static void main(String[] args) {
             QApplication.initialize(args);

             TestWidget testTestWidget = new TestWidget(null);
             testTestWidget.show();
             testTestWidget.changeLabel();

             QApplication.exec();
         }

         public TestWidget(QWidget parent){
             l = new QLabel("testLabelString",this);

             l.move(20,20);
             l.show();
             l.resize(100,20);
             resize(400,300);
         }

         public void changeLabel() {
             new ThreadClass(l).start();
         }
}




ThreadClass class:


package test;

import com.trolltech.qt.gui.QLabel;

public class ThreadClass extends Thread {

          private QLabel l;

          public ThreadClass(QLabel l) {
              this.l = l;
          }

          public void run() {
              try {
                  sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              l.moveToThread(currentThread()); /* line 19 */
              l.setText("testLabelStringChangedByThread");
          }
}



--
Tomasz 'Trog' Welman


More information about the Qt-jambi-interest mailing list