Using qui rather than rbuic for qtdesigner .ui files


#1

I’m trying to load .ui files at runtime using the qui extension and
WidgetFactory rather than pre-translating the .ui XML file to ruby with
rbuic. Here’s a simple program:

  require 'Qt'
  require 'qui'
  a = Qt::Application.new(ARGV)
  w = QUI::WidgetFactory.create "sampledialog.ui"
  w.show()
  a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
  a.exec()

This works just fine in that it dynamically loads and presents the .ui
file as a window. However, how do I connect signals and slots? I
understand the process of method override when doing it the rbuic way,
but this doesn’t seem to be working with WidgetFactory. The signals and
slots (but not their handling) are defined in the .ui file. Could
someone give this noob an example of how to implement a slot method? I
tried looking at some C++ examples, but I’m just not making the
connection to ruby. The .ui file is below. It just has two buttons,
Test and Close.

Thanks,

David

SampleDialogUI


SampleDialogUI



0
0
155
45



Sample Dialog


true



unnamed



closeButton


Close


Close this dialog




testButton


Test


Test command execution






testButton
clicked()
SampleDialogUI
testButton_clicked()


closeButton
clicked()
SampleDialogUI
closeButton_clicked()



testButton_clicked()
closeButton_clicked()



#2

Alle mercoledì 14 gennaio 2009, David Brunell ha scritto:

  a.exec()

Thanks,

David

Not tested, because I’m using Qt 4 while, I think, you’re using Qt 3.
The idea
is to create a custom widget class and to add the widget returned by
WidgetFactory.create as child widget. You shouldn’t define custom slots
in the
ui file, since there’s no way to implement them. Any custom slot you’d
define
in the ui file is instead defined in the custom widget. Here’s a simple
example which should work:

require ‘Qt’
require ‘qui’

class MainWidget < Qt::Widget

slots ‘test()’

def initialize
@widget = QUI::WidgetFactory.create “sampledialog.ui”
self.layout = Qt::VBoxLayout.new
self.layout.add_widget @widget
connect @widget.child(‘testButton’), SIGNAL(‘clicked()’), self,
SLOT(‘test()’)
end

def test
puts “test button clicked”
end

end

app = Qt::Application.new ARGV
w = MainWidget.new
w.show
a.connect(a, SIGNAL(‘lastWindowClosed()’), a, SLOT(‘quit()’))
a.exec

I hope this helps

Stefano


#3

Stefano C. wrote:

require ‘Qt’
require ‘qui’

class MainWidget < Qt::Widget

slots ‘test()’

def initialize
@widget = QUI::WidgetFactory.create “sampledialog.ui”
self.layout = Qt::VBoxLayout.new
self.layout.add_widget @widget
connect @widget.child(‘testButton’), SIGNAL(‘clicked()’), self,
SLOT(‘test()’)
end

def test
puts “test button clicked”
end

end

app = Qt::Application.new ARGV
w = MainWidget.new
w.show
a.connect(a, SIGNAL(‘lastWindowClosed()’), a, SLOT(‘quit()’))
a.exec

Many thanks for your kind reply. I get the following error when running
your suggested code:

quitest2.rb:10:in method_missing': undefined methodlayout=’ for
:MainWidget (NoMethodError)

I somewhat understand this error, but upon commenting out the two
self.layout lines, I then get the following error:

quitest2.rb:12:in method_missing': super: no superclass methodconnect’(NoMethodError)

If I change “connect” to “Qt::Object.connect” (this is what the rbuic
compiler would generate) the error persists. Any ideas?

Thanks,

David


#4

Stefano C. wrote:

Sorry, I forgot one line. The initialize method should be:

def initialize
super # this is the new part

Well, now I get no errors. However, the dialog which appears is a blank
dialog, not the one which I designed with Qt designer. Any thoughts? I
can always go back to the rbuic compiler if I have to. Here is my code
at this point:

########################
require ‘Qt’
require ‘qui’

class MainWidget < Qt::Widget

slots ‘test()’

def initialize
super
@widget = QUI::WidgetFactory.create “sampledialog.ui”
set_layout = Qt::VBoxLayout.new
set_layout.add_widget @widget
connect @widget.child(‘testButton’), SIGNAL(‘clicked()’), self,
SLOT(‘test()’)
end

def test
puts “test button clicked”
end

end

a = Qt::Application.new ARGV
w = MainWidget.new
w.show
a.connect(a, SIGNAL(‘lastWindowClosed()’), a, SLOT(‘quit()’))
a.exec
################################

Many thanks,

David


#5

Ok, I got it working. Here is my code:

##########################################
require ‘Qt’
require ‘qui’

class MainWidget < Qt::Widget

slots ‘test()’, ‘close()’

def initialize(app)
super()
@app = app
@widget = QUI::WidgetFactory.create “sampledialog.ui”
connect @widget.child(‘testButton’), SIGNAL(‘clicked()’), self,
SLOT(‘test()’)
connect @widget.child(‘closeButton’), SIGNAL(‘clicked()’), self,
SLOT(‘close()’)
@widget.show
end

def test
puts “test button clicked”
end

def close
@app.exit(0)
end

end

app = Qt::Application.new ARGV
dialog = MainWidget.new(app)
app.exec
######################################

The .ui file is the same as in the original post. I’m not sure this is
the most elegant way to make it work, so if you have any ideas on how to
clean it up, I’m open to suggestions.

One other question…what about Qt4? I think I need rbuic4 if I am
going to pre-compile the ui. Is there a qui4 for dynamic loading?

Kind regards,

David


#6

Alle giovedì 15 gennaio 2009, David Brunell ha scritto:

self.layout = Qt::VBoxLayout.new

:MainWidget (NoMethodError)

I somewhat understand this error, but upon commenting out the two
self.layout lines, I then get the following error:

quitest2.rb:12:in method_missing': super: no superclass methodconnect’(NoMethodError)

Sorry, I forgot one line. The initialize method should be:

def initialize
super # this is the new part
@widget = QUI::WidgetFactory.create “sampledialog.ui”
self.layout = Qt::VBoxLayout.new
self.layout.add_widget @widget
connect @widget.child(‘testButton’), SIGNAL(‘clicked()’), self,
SLOT(‘test()’)
end

This will solve the second problem and most likely also the first. If
the firs
remains, try replacing

self.layout =

set_layout

Stefano


#7

Alle mercoledì 21 gennaio 2009, Stefano C. ha scritto:

file.close
puts "testing"

end

end

app = Qt::Application.new ARGV
dlg = Dlg.new
dlg.exec
app.exec

Stefano

My previous code contained an error: the clicked() signal of @close_btn
should
be connected to the quit() slot of Qt::Application.instance, not to the
close() signal (which it doesn’t exist).

Also, to work around the bug I spoke about, it’s enough to replace
Qt::PushButton with Qt::Object in the two calls to findChild.

Stefano


#8

Alle lunedì 19 gennaio 2009, David Brunell ha scritto:

One other question…what about Qt4? Â I think I need rbuic4 if I am
going to pre-compile the ui. Â Is there a qui4 for dynamic loading?

The following should work using a recent svn revision from kdebindings.
However, it doesn’t because findChild fails. I think it’s a bug and I’ll
try
to contact the developers to

require ‘Qt4’
require ‘qtuitools’

class Dlg < Qt::Dialog

slots ‘test()’

def initialize parent = nil
super
file = Qt::File.new File.expand_path(’~/temp/test_ui.ui’)
file.open(Qt::File::ReadOnly)
loader = Qt::UiLoader.new
@widget = loader.load file, self
file.close
self.layout = Qt::VBoxLayout.new self
layout.add_widget @widget
@close_btn = @widget.findChild(Qt::PushButton, ‘close_btn’)
@test_btn = @widget.findChild(Qt::PushButton, ‘test_btn’)
connect @close_btn, SIGNAL(‘clicked()’), Qt::Application.instance,
SLOT(‘close()’)
connect @test_btn, SIGNAL(‘clicked()’), self, SLOT(‘test()’)
end

def test
puts “testing”
end

end

app = Qt::Application.new ARGV
dlg = Dlg.new
dlg.exec
app.exec

Stefano