[Kde-bindings] QT TextEdit refresh
Richard Dale
rdale at foton.es
Tue Jul 24 16:48:49 UTC 2007
On Tuesday 24 July 2007, Carl Bourne wrote:
> 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()
The Qt::TextEdit won't get repainted until you return to the event loop. So if
you do everything inside to go() method you won't return to the event loop
until you've done the 50 iterations.
You need to do the 'Generating files' processing in another method, that is
called once before control returns to the event loop like this for instance:
class MyWidget < Qt::Widget
slots :go, :generate
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.font = Qt::Font.new('Arial', 12, Qt::Font::Bold)
connect(quit, SIGNAL(:clicked)) {@textedit.clear(); go() }
end
def go
# @textedit.clear()
@textedit.text = "Generating files:-"
@count = 0
generate
end
def generate
@count += 1
@textedit.append("Generating file: "+ at count.to_s)
puts "hello"
if @count == 50
@textedit.append("Done")
else
@timer = Qt::Timer.singleShot(100, self, SLOT(:generate))
end
end
end
PS: The two pieces of your code didn't seem to quite match as there was no
Dialog class in dialog.rb..
-- Richard
More information about the Kde-bindings
mailing list