Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Last active March 9, 2024 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Papierkorb/651fab1d02d69a9521ff5a12169b747d to your computer and use it in GitHub Desktop.
Save Papierkorb/651fab1d02d69a9521ff5a12169b747d to your computer and use it in GitHub Desktop.
Qt Bindings WIP
require "./qt/version"
require "./qt/glue"
require "./qt/binding"
require "./qt/*"
qApp = Qt::Application.new # Decent #initialize
window = Qt::MainWindow.new # More classes!
# This method was once called `setWindowTitle()` in C++.
# `#window_title=` looks better though. The generator knows this.
window.window_title = ".: Crystal/Qt :."
container = Qt::Widget.new # This is Qt noise..
window.central_widget = container
layout = Qt::VBoxLayout.new
container.layout = layout
# Oh hey, what's this?
class MyLabel < Qt::Label
# Ever heard of C++ virtual methods?
def has_height_for_width : Bool
true
end
# Yeah, overwriting them works OOTB now!
# It's decently portable too, it'll just work with any modern C++ compiler.
# ARM and friends should work too, can't test this though.
def height_for_width(width : Int32) : Int32
width
end
end
# Quickly build a simple UI
btn_box = Qt::HBoxLayout.new
btn_up = Qt::PushButton.new "Increment"
btn_down = Qt::PushButton.new "Decrement"
label = MyLabel.new "Counter: 0"
# We end up with Crystal classes for the wrappers, so it's no issue at all to
# Crystal-ize the API a bit more, apart from what the generator already does.
btn_box << btn_up << btn_down
layout << btn_box << label
# We will in-/de-crement `count` on each press on one of the buttons.
# Simple enough - Point is: Blocks work 100% as expected.
count = 0
btn_up.on_pressed do
count += 1
label.text = "Counter: #{count}"
end
btn_down.on_pressed do
count -= 1
label.text = "Counter: #{count}"
end
# Ah well, let's kick this off
window.show # Open the window
Qt::Application.exec # Start Qt's main loop
# And this is where it's at right now. More to come.
require "./qt/version"
require "./qt/glue"
require "./qt/binding"
require "./qt/*"
arguments = ARGV.clone
arg_count = arguments.size
qApp = Qt::Application.new(pointerof(arg_count), arguments.map(&.to_unsafe).to_unsafe)
btn = Qt::PushButton.new("Click Me!")
btn.pressed do
puts "You pushed da button!"
end
btn.show
Qt::Application.exec
require "./qt/version"
require "./qt/glue"
require "./qt/binding"
arguments = ARGV.clone
arg_count = arguments.size
# Looks a bit funky, but eh, nothing a small hand-written #initialize can't solve!
qApp = Qt::Application.new(pointerof(arg_count), arguments.map(&.to_unsafe).to_unsafe)
label = Qt::Label.new("Hello, World!")
label.window_title = "Crystal \\m/"
label.show
Qt::Application.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment