Source

miuler-demo-qtthreads / src / main / scala / com / miuler / demo / qthreads / Main.scala

Full commit
package com.miuler.demo.qthreads

import com.trolltech.qt.gui._
import com.trolltech.qt.core.{QObject, QRunnable}
import com.trolltech.qt.QThread
import com.trolltech.qt.core.Qt.ConnectionType


object Main {
  def main(argv: Array[String]) {
    QApplication.initialize(argv)
    new WindowMain show()
    QApplication.exec()
  }
}

class WindowMain extends QWidget {

  private val _label = new QLabel("Esta es la ventana principal que capturara los eventos")
  private val _layout = new QGridLayout()
  _layout.addWidget(_label)
  _layout.addWidget(new QPushButton("Hola"))
  setLayout(_layout)

  private val _demoRunnable = new DemoRunnable
  private val _demoThread = new QThread(_demoRunnable)
  _demoRunnable.moveToThread(_demoThread)
  _demoRunnable.messageSignal.connect(this, "updateLabel(String)", ConnectionType.QueuedConnection)
  _demoThread.start()
  println("Thread running")

  def updateLabel(msg: String) {
    _label.setText(msg)
  }

}

class DemoRunnable extends QObject with Runnable {

  val messageSignal = new Signal1[String]

  def run() {
    println("DemoRunnable")
    Thread.sleep(5000)
    messageSignal.emit("Hola esta es una prueba")
  }
}






/*
public class QtThreadTest extends QDialog {

  private QLabel label = new QLabel("Hello world !!!");

  private class MyThread extends QObject implements Runnable {

    public QObject.Signal1<String> messageSignal = new
        QObject.Signal1<String>();

    public void run() {

      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      this.messageSignal.emit("Hello world from MyThread !!!");
    }
  }

  private void updateLabel(String message) {

    this.label.setText(message);
  }

  public QtThreatTest() {

    QGridLayout layout = new QGridLayout();
    layout.addWidget(label);
    this.setLayout(layout);

    MyThread mt = new MyThread();
    mt.messageSignal.connect(this, "updateLabel(String)",
      ConnectionType.QueuedConnection);

    QThread t = new QThread(mt);
    t.start();
    System.out.println("Thead running...");
  }

  public static void main(final String[] args) {
    QApplication.initialize(args);
    new QtThreadTest().show();
    QApplication.exec();
  }
}
*/