# rubyQT

**URL:** <https://www.ruby-forum.com/t/rubyqt/110256>\
**Category:** Ruby\
**Created:** [July 24, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/rubyqt/110256 "2007-07-24T18:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [July 24, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/rubyqt/110256/1 "2007-07-24T18:20:48Z")

</div>

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()

* * *

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/rubyqt/110256/2 "2007-07-26T00:45:49Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/rubyqt/110256/3 "2007-07-26T00:46:32Z")

</div>

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.
