From: richard.j.dale@... Date: 2009-05-12T21:05:04+09:00 Subject: Re: (mandriva,eclipse,Qt) elementary problem : signals and slots On May 12, 7:17 am, Stefano Crocco wrote: > On Monday 11 May 2009, lolveley wrote: > > > > > |hi, > > | > > |I would like to make a little program using Qt : it's a displayer / > > |converter between farenheit and celsius degrees. > > |I also want to use QtDesigner. > > | > > |I made a little widget with QtDesigner, and this widget contains two > > |dials(dial and dial_2) and two spinboxes(kind of textboxes) (spinbox and > > |spinbox_2). > > |There are some connections : see below for the ruby file I obtained with > > |the tool rbuic4 (which converts the ".ui" file into ".rb"). > > |my problem is this : I want that some changes made in dial_2 modify dial > > |(and then spinbox), eg : > > |    0 celsius degrees => 32 farenheit degrees. > > | > > |here is the file made by rbuic4 and after this my try to modify the slot > > |setValue (but it doesn't run: the value of dial is always the same as > > |the one of dial_2. > > I don't know why it doesn't work, as I never used the designer to make > signal/slot connections. I'd say it should work, but there might be some > limitations which I'm not aware of. My suggestion is to use a custom widget > class instead of a generic Qt::Widget and to implement the slot which converts > the values there. Here's how the tmp_dsc_form.rb could look like: > > require 'Qt4' > require 'temp.rb' > > class MyWidget < Qt::Widget > >   slots 'change_values(int)' > >   def initialize >     super >     @ui = Ui_Form.new >     @ui.setupUi self >     connect @ui.dial_2, SIGNAL('valueChanged(int)'), self, > SLOT('change_values(int)') >   end > >   def change_values val >     new_value = (1.8*val+32).to_i >     @ui.spinBox.value = new_value >     @ui.dial.value = new_value >   end > > end > > une_appli=Qt::Application.new(ARGV) > un_wdg = MyWidget.new > un_wdg.show > une_appli.exec > > Of course, if you use this you can remove the connections made from the > designer. I'm not sure if this solves the problem of making the changes bi- directional because if you use the 'valueChanged(int)' signal, as you will end up with a loop because each dial will try to change the other. I think the original rbuic4 generated code was nearly correct, but it should use 'sliderMoved(int)' signals in one dial connected to 'setValue(int)' in the other dial because that would loop. And it needs an extra connection so that @dial_2 is changed when @dial is changed. Qt::Object.connect(@dial, SIGNAL('sliderMoved(int)'), @dial_2, SLOT ('setValue(int)')) Qt::Object.connect(@dial_2, SIGNAL('sliderMoved(int)'), @dial, SLOT ('setValue(int)')) But that wouldn't convert the values. So instead it needs an intermediate Qt::Object (doesn't need to be a Qt::Widget) to convert the values as Stefano suggests. -- Richard