我还没有找到使用Clojure REPL在网络上与Qt一起使用的解决方案。
基本上问题是,一旦调用QApplication/exec以显示UI,REPL就会挂起。您无法通过C-c C-c返回REPL,并且关闭活动的Qt窗口似乎会终止整个Clojure进程。
现在,除非代理程序在创建Qt小部件的完全相同的线程中运行,否则无法从代理程序中简单地调用QApplication/processEvents。我花了两天时间才弄清楚这一点,我看到其他人也有同样的问题,但没有解决方案。 因此,这是我的解决方案,代码如下:
基本上它使用ScheduledThreadPoolExecutor类来执行所有Qt代码。您可以使用with-gui-thread宏,以便更轻松地从线程内调用函数。这使得可以动态更改Qt UI,无需重新编译。
现在,除非代理程序在创建Qt小部件的完全相同的线程中运行,否则无法从代理程序中简单地调用QApplication/processEvents。我花了两天时间才弄清楚这一点,我看到其他人也有同样的问题,但没有解决方案。 因此,这是我的解决方案,代码如下:
(add-classpath "file:///usr/share/java/qtjambi.jar")
(ns qt4-demo
(:import (com.trolltech.qt.gui QApplication QPushButton QFont QFont$Weight)
(com.trolltech.qt.core QCoreApplication)
(java.util Timer TimerTask)
(java.util.concurrent ScheduledThreadPoolExecutor TimeUnit))
(:require swank.core))
(defn init []
(QApplication/initialize (make-array String 0)))
(def *gui-thread* (new java.util.concurrent.ScheduledThreadPoolExecutor 1))
(def *gui-update-task* nil)
(def *app* (ref nil))
(defn update-gui []
(println "Updating GUI")
(QApplication/processEvents))
(defn exec []
(.remove *gui-thread* update-gui)
(def *gui-update-task* (.scheduleAtFixedRate *gui-thread* update-gui 0 150 (. TimeUnit MILLISECONDS))))
(defn stop []
(.remove *gui-thread* update-gui)
(.cancel *gui-update-task*))
(defmacro qt4 [& rest]
`(do
(try (init) (catch RuntimeException e# (println e#)))
~@rest
))
(defmacro with-gui-thread [& body]
`(.get (.schedule *gui-thread* (fn [] (do ~@body)) (long 0) (. TimeUnit MILLISECONDS))))
(defn hello-world []
(with-gui-thread
(qt4
(let [app (QCoreApplication/instance)
button (new QPushButton "Go Clojure Go")]
(dosync (ref-set *app* app))
(doto button
(.resize 250 100)
(.setFont (new QFont "Deja Vu Sans" 18 (.. QFont$Weight Bold value)))
(.setWindowTitle "Go Clojure Go")
(.show)))))
(exec))
基本上它使用ScheduledThreadPoolExecutor类来执行所有Qt代码。您可以使用with-gui-thread宏,以便更轻松地从线程内调用函数。这使得可以动态更改Qt UI,无需重新编译。