rubyQT

Hi,

I wonder if you can help. I’m currently exploring rubyqt in conjunction
with the latest QT4 libraries. However, I can’t seem to get a TextEdit
widget to refresh properly. My code runs correctly when I click the
button that has a ruby function assigned to it, but the TextEdit field
does not update until the ruby function completes.

Here’s the code I’m using:-


main.rb

require ‘Qt’
require ‘dialog.rb’
app = Qt::Application.new(ARGV)
dialog = Dialog.new
dialog.exec


dialog.rb

class MyWidget < Qt::Widget
slots ‘go()’
def initialize(parent = nil)
super(parent)
setFixedSize(500, 500)
@textedit = Qt::TextEdit.new(self)
@textedit.setGeometry(5, 50, 480, 300)

quit = Qt::PushButton.new(('Generate'), self)
quit.setGeometry(5, 5, 100, 30)
quit.setFont(Qt::Font.new('Arial', 12, Qt::Font::Bold))
connect(quit, SIGNAL(:clicked)) {@textedit.clear();  go() }

end

def go
# @textedit.clear()
@textedit.setText(“Generating files:-”)
count = 0
50.times do
count+=1
sleep 0.1
@textedit.append("Generating file: "+count.to_s)
puts “hello”
end
@textedit.append(“Done”)
end
end
app = Qt::Application.new(ARGV)
widget = MyWidget.new()
widget.show()
app.exec()


I wonder if you can help. I’m currently exploring rubyqt in conjunction
with the latest QT4 libraries. However, I can’t seem to get a TextEdit
widget to refresh properly. My code runs correctly when I click the
button that has a ruby function assigned to it, but the TextEdit field
does not update until the ruby function completes.

Right. When your go() slot is running, that’s all that’s happening in
your program.
Calls to the @textedit are put into the queue and will update and
refresh the next
available opportunity the event loop runs, but that won’t happen until
you finish
running your slot.

If you need the program to be more responsive, you need to look into
using a
Qt::Timer to call a slot repetetively. That way Qt can go back to
handling all of
the events in the event loop in between timer calls.

HTH,
Caleb

Hi Carl,

Assuming that the problem your having is that you type and not text
shows up, I was not able to reproduce the issue. I did have to make
the following changes to get your code to run at all:


my_widget.rb

class MyWidget < Qt::Widget
slots ‘go()’
def initialize(parent = nil)
super(parent)
setFixedSize(500, 500)
@textedit = Qt::TextEdit.new(self)
@textedit.setGeometry(5, 50, 480, 300)

quit = Qt::PushButton.new((‘Generate’), self)
quit.setGeometry(5, 5, 100, 30)
quit.setFont(Qt::Font.new(‘Arial’, 12, Qt::Font::Bold))
connect(quit, SIGNAL(:clicked)) {@textedit.clear(); go() }
end

def go
# @textedit.clear()
@textedit.setText(“Generating files:-”)
count = 0
50.times do
count+=1
sleep 0.1
@textedit.append("Generating file: "+count.to_s)
puts “hello”
end
@textedit.append(“Done”)
end
end
if $0 == FILE
a = Qt::Application.new(ARGV)
w = MyWidget.new
a.mainWidget = w # IMPORTANT ADDITION
w.show
a.exec
end

You’ll notice that I’m only using one file (my_widget.rb) instead of
the two file structure you had before. The main.rb would work like
this:


main.rb

require ‘my_widget’
a = Qt::Application.new(ARGV)
w = MyWidget.new
a.mainWidget = w
w.show
a.exec

'exec’ing in multiple places, instantiating more than one instance of
Qt::Application and not adding the main widget to the application
instance are all issues I saw in your code that are fixed in the
above.

Hope That Helps,
Rob K.