如何将Clojure REPL与Qt Jambi一起使用?

11
我还没有找到使用Clojure REPL在网络上与Qt一起使用的解决方案。 基本上问题是,一旦调用QApplication/exec以显示UI,REPL就会挂起。您无法通过C-c C-c返回REPL,并且关闭活动的Qt窗口似乎会终止整个Clojure进程。
现在,除非代理程序在创建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,无需重新编译。

是的,我也不得不做同样的事情。 - levand
我对QT一无所知。但是你为什么要这样做呢?Clojure可以访问Swing,这是一个非常强大和多功能的GUI框架。你是要连接到已经存在的QT GUI吗? - Carl Smotricz
QT 在很多方面都比 Swing 更好,包括性能和本地外观。 - levand
Qt 在我看来也有更加清晰的 API。(很难想象出比 Swing 更丑陋的 API。)Qt 应用程序与 Linux 桌面环境的集成也比 Swing 应用程序要好得多。 - Brian Carper
2个回答

5

如果您想从REPL更改Qt小部件,QApplication/invokeLaterQApplication/invokeAndWait可能是您需要的。您可以将它们与代理一起使用。给定以下内容:

(ns qt4-demo
  (:import (com.trolltech.qt.gui QApplication QPushButton)
           (com.trolltech.qt.core QCoreApplication)))

(def *app* (ref nil))
(def *button* (ref nil))
(def *runner* (agent nil))

(defn init [] (QApplication/initialize (make-array String 0)))
(defn exec [] (QApplication/exec))

(defn hello-world [a]
  (init)
  (let [app (QCoreApplication/instance)
        button (doto (QPushButton. "Go Clojure Go") (.show))]
    (dosync (ref-set *app* app)
            (ref-set *button* button)))
  (exec))

然后从REPL:

qt4-demo=> (send-off *runner* hello-world)
#<Agent@38fff7: nil>

;; This fails because we are not in the Qt main thread
qt4-demo=> (.setText @*button* "foo")
QObject used from outside its own thread, object=QPushButton(0x8d0f55f0) , objectThread=Thread[pool-2-thread-1,5,main], currentThread=Thread[main,5,main] (NO_SOURCE_FILE:0)

;; This should work though
qt4-demo=> (QApplication/invokeLater #(.setText @*button* "foo"))
nil
qt4-demo=> (QApplication/invokeAndWait #(.setText @*button* "bar"))
nil

非常好。我喜欢那个。谢谢! - MHOOO

3
我曾在我的博客(德语)Clojure邮件列表上介绍了如何使用SLIME完成这个操作。关键是在Emacs端定义适当的函数并告诉SLIME在发出请求时使用这些函数。重要的是,这使您免于在调用Qt代码时进行特殊的咒语。
鉴于我们在讨论Lisp,解决方案似乎很明显:修改SLIME!以下代码可在.emacs中插入(在完全加载SLIME的某一点),以注册三个新的Emacs-Lisp函数供交互使用。您可以将它们绑定到任何键上,或者在应用程序启动后只需将slime-send-through-qapplication变量设置为t而不必担心键绑定。无论哪种方式,都应该通过QCoreApplication/invokeAndWait间接进行REPL提交和C-M-x样式的交互式评估。祝玩得开心!
(defvar slime-send-through-qapplication nil) 
(defvar slime-repl-send-string-fn (symbol-function 'slime-repl-send- 
string)) 
(defvar slime-interactive-eval-fn (symbol-function 'slime-interactive- 
eval)) 

(defun qt-appify-form (form) 
  (concatenate 'string    ;'
               "(let [return-ref (ref nil)] " 
               "  (com.trolltech.qt.core.QCoreApplication/invokeAndWait " 
               "   (fn [] " 
               "     (let [return-value (do " 
               form 
               "          )] " 
               "       (dosync (ref-set return-ref return-value))))) " 
               "  (deref return-ref))")) 

(defun slime-interactive-eval (string) 
  (let ((string (if slime-send-through-qapplication 
                    (qt-appify-form string) 
                    string))) 
    (funcall slime-interactive-eval-fn string))) 

(defun slime-repl-send-string (string &optional command-string) 
  (let ((string (if slime-send-through-qapplication 
                    (qt-appify-form string) 
                    string))) 
    (funcall slime-repl-send-string-fn string command-string))) 

(defun slime-eval-defun-for-qt () 
  (interactive) 
  (let ((slime-send-through-qapplication t)) 
    (slime-eval-defun))) 

(defun slime-repl-closing-return-for-qt () 
  (interactive) 
  (let ((slime-send-through-qapplication t)) 
    (slime-repl-closing-return))) 

(defun slime-repl-return-for-qt (&optional end-of-input) 
  (interactive) 
  (let ((slime-send-through-qapplication t)) 
    (slime-repl-return end-of-input))) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接