Calling lqt apps as an extension to another program

瀏覽次數:36 次
跳到第一則未讀訊息

Saptarshi Joy Guha

未讀,
2013年6月28日 下午5:22:272013/6/28
收件者:lqt-bi...@googlegroups.com
Hello,

Not the best subject, so let me explain. I am demonstrating the ease of use of using luajit and terra as an extension language to R.
One example will include using lqt for displays

E.g. inside R

doTerraFile("myfile.t")
doTerra("doTest",1)

doTest is a lua function

contents of myfile.t:

require 'qtcore'
require 'qtgui'
function doTest(x)
   local app = QApplication.new(2, {'lua','-nograb'})
   local w = QWidget.new()
   function w:closeEvent(e)
      print('Closing!')
   end
   w:show()
   app.exec()
   print("HELLEOE")
   return nil
end

This works quite nicely. However, app.exec grabs the event loop and only returns upon close. Fair enough.

However, since I am embedding inside R, it would be nice to be able to return control to R and allow the app to run alongside.

Would such a thing enable it? E.g. a
app = nil
function makeEmbedApp(x)
  app = QApplication.new(#x, x)

    Call-to-C++ function-called-RunInT(app)

RunInT creates a subclass of QThread, and repeatedly calls processevents on app.

Thus the presumable the above code would still work and the control would be returned to R.
However, this implies lqt contain that bit of C++ code (though I could write my own, other users of lqt wouldn't have it unless it comes with lqt)

Is this sensible or just plain wrong?
Cheers
Saptarshi

Michal Kottman

未讀,
2013年7月1日 下午5:04:272013/7/1
收件者:lqt-bi...@googlegroups.com
I looked at this, and it seems it is possible to run your own version of the event loop. [1] You just have to write the event loop yourself.


After some experimentation, this is what I came up with:

require 'qtcore'
require 'qtgui'

local App = QApplication(1, {"Test"})
local Btn = QPushButton("Quit!")
local running = true

Btn:show()
Btn:connect('2clicked()', function()
running = false
end)

while running do
QCoreApplication.processEvents() -- call this inside the event loop
end

I am not sure how it will behave in Terra, but feel free to try it out.

Saptarshi Guha

未讀,
2013年7月7日 晚上10:48:552013/7/7
收件者:lqt-bi...@googlegroups.com
Yes something similar. I followed the usual approach to integrating within R's eventloop.
The code looks like (note, I'm using Terra). qtinit is called first.

Thanks!

require 'qtcore'
require 'qtgui'
pthread = terralib.includec("pthread.h")
local ifd = global(int)
local ofd = global(int)
local fired=global(int,0)
local qtwriterthread = global(pthread.pthread_t)
local App = nil
local mu = {}
function processQTEvents(x)
   QCoreApplication.processEvents()
end
terra QTEventLoopHandler(data : &uint8)
   var buf : uint8[16]
   unistd.read(ifd,buf,16)
   processQTEvents()
   fired=0
end
terra mywriter(data : &uint8) : &uint8
   var buf : uint8[16];
   while true do
      unistd.usleep(10000)
      if fired == 0  then
     fired = 1
     buf[0]=0
     var s = unistd.write(ofd, buf, 1);
      end
   end
end
terra QTEventLoopInit()
   var fds : int[2]
   if unistd.pipe(fds) == 0  then
      ifd,ofd = fds[0],fds[1]
      Rinternals.addInputHandler(R.constants.InputHandlers,ifd,QTEventLoopHandler,31)
      Rinternals.setCStackLimit(-1)
      pthread.pthread_create(&qtwriterthread,nil,mywriter,nil);
   end
end
function qtinit(a)
   App = QApplication(1, {"qtbase", "-nograb"})
   QTEventLoopInit()
end

function doTest2(x)
   mu.Btn = QPushButton("Quit!")
   local running  = true
   mu.Btn:show()
   mu.Btn:connect('2clicked()',
          function()
             print("BYeeee\n")
             mu.Btn:close()
           end)
   return nil
end




--
 
---
You received this message because you are subscribed to a topic in the Google Groups "lqt-bindings" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lqt-bindings/gvqDGz3Izjc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lqt-bindings...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

回覆所有人
回覆作者
轉寄
0 則新訊息