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.
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:
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:
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
################################
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?
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
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.
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