ryanmelt / qtbindings

An easy to install gem version of the Ruby bindings to Qt
http://github.com/ryanmelt/qtbindings
Other
340 stars 63 forks source link

emitting signal makes app freeze #90

Closed xxjapp closed 10 years ago

xxjapp commented 10 years ago

My app freezes when I use emit to update status on GUI.

My test environment

The demo app is present below.

#!/usr/bin/env ruby
# encoding: UTF-8
#

require 'Qt'

class App < Qt::MainWindow
    signals 'test()'
    slots   'on_test()'

    def initialize
        super

        @label = Qt::Label.new
        self.centralWidget = @label

        self.show

        connect self, SIGNAL('test()'), SLOT('on_test()')
        start_count
    end

    def start_count
        Thread.new do
            loop {
                emit test()
            }
        end
    end

    def on_test()
        @label.text = @label.text.to_i + 1
    end
end

app = Qt::Application.new(ARGV)
App.new
app.exec
jmthomas commented 10 years ago

I think you're just overwhelming the system. I added a sleep 0.1 after your emit test and it works for me.

xxjapp commented 10 years ago

@jmthomas That is just a demo app. In my real app, the emits are not so quick as the demo. I also have tested sleep solution both in the demo and my real app. Both of them still suffer freeze once in a while.

jmthomas commented 10 years ago

You might need to buffer up the emits or do something to ensure they aren't coming out too fast. Also make sure to not access QT GUI methods from another thread. If you need to do this wrap your code with Qt.execute_in_main_thread(true) { }

xxjapp commented 10 years ago

@jmthomas Thanks for your answer. I'm sure that there is no access QT GUI methods from another thread.

Now I use a timer in main thread to update the GUI status and a queue to keep data which need to be sent to main thread. I think the queue is something like the buffer you said.

By the way, my app is here: https://github.com/xxjapp/auto_enc