Created
July 17, 2012 13:35
-
-
Save Mon-Ouie/3129427 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'Qt4' | |
def add_shortcut(widget, key, &block) | |
seq = Qt::KeySequence.new(key) | |
Qt::Shortcut.new(seq, widget) do |shortcut| | |
shortcut.connect(SIGNAL("activated()")) do | |
puts "running #{seq.inspect}" | |
widget.instance_eval(&block) | |
end | |
shortcut.context = Qt::WidgetWithChildrenShortcut | |
end | |
end | |
def popup(message) | |
Qt::MessageBox.new { |box| | |
box.text = message | |
}.exec | |
end | |
class Editor < Qt::TextEdit | |
def initialize(parent = nil) | |
super(parent) | |
add_shortcut(self, "Return") { popup "From Editor" } | |
@fiber = Fiber.new { | |
# if you uncomment the require, Return will input a line break | |
# instead of showing the popup. | |
# | |
# Happens with seemingly any gem that wasn't activated before. | |
# require 'yard' | |
} | |
@fiber.resume | |
end | |
end | |
class Window < Qt::MainWindow | |
def initialize | |
super() | |
self.central_widget = Editor.new(self) | |
end | |
end | |
class EventFilter < Qt::Object | |
protected | |
def eventFilter(obj, event) | |
(event.type == Qt::Event::ShortcutOverride).tap { |c| | |
puts "event filtered" if c | |
} | |
end | |
end | |
app = Qt::Application.new(ARGV) | |
app.install_event_filter EventFilter.new | |
win = Window.new | |
win.show | |
app.exec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment