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
20.5k29 gold badges117 silver badges308 bronze badges
asked Jul 29, 2012 at 23:41

1 Answer 1

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/

answered Sep 17, 2012 at 14:41
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.