3

I'd like to make a graphical user interface for my script, instead of running it from the console. I'm aware there's a wealth of UI libraries for Ruby, but I'm quite familiar with HTML and CSS and I'd like to use them for building an interface. So the question is:

Is it possible to use a HTML rendering library to make such an UI? From what I understand, it's relatively easy to put in a HTML rendered view of something, but is it possible to communicate back with the script? Like when I push that big red button, it actually tells the script to act on it? Obviously it's possible if the script run on the server side, but I'd like to run it as a desktop application.

gnat
  • 21,442
  • 29
  • 112
  • 288
mjr
  • 31
  • 1

1 Answers1

2

Qt has a Webkit module. You should be able to create a a UI using those to do what you want.

Here is an example of a dynamic page using QtRuby

require 'Qt4'
require 'qtwebkit'

class A < Qt::WebView
  slots 'mySlot(QUrl)'
  def initialize()
super()
connect(self, SIGNAL('linkClicked(QUrl)'), self, SLOT('mySlot(QUrl)'))
  end
  def mySlot(url)
if (url.path == 'quit')
  puts "quit"
  exit()
elsif (url.path == 'cont')
  setHtml <<EOP
<html>
 <head>
  <title>test</title>
 </head>
 <body>
  <div>
This is a test. That works</br>
<h2><a id='click' href="quit" onclick="quit()">Quit</a></h2>
  </div>
 </body>
</html>
EOP
 end
  end
end

Qt::Application.new(ARGV) do
   A.new() do |view|
view.page.setLinkDelegationPolicy(Qt::WebPage::DelegateAllLinks)
view.setHtml <<EOP
<html>
 <head>
  <title>test</title>
 </head>
 <body>
  <div>
This is a test.</br>
<a id='click' href="quit" onclick="quit()">quit</a>
<a id='click' href="cont" onclick="quit()">Click on me!</a>
  </div>
 </body>
</html>
EOP
view.show
  end
  exec
end

The only down side is that you will, in most cases, have to read the Qt C++ documentation and convert it into Ruby. At least that is my experiance.

http://doc.qt.nokia.com/4.7-snapshot/qtwebkit.html

http://zetcode.com/gui/rubyqt/

gam3
  • 783
  • 6
  • 13