QtRuby: Check Box not toggling - ruby

I have created a Checkbox with label AVS . When I click the checkbox, I need the below block of code to be executed. I am neither getting any error, nor is the toggle working.
require 'Qt'
class Auth < Qt::Widget
slots 'on_toggled(bool)'
def initialize(parent=nil)
super(parent)
setWindowTitle("Org");
setFixedSize 750,530
avs_ui
show
end
def avs_ui
#label = Qt::Label.new "AVS?: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
cb = Qt::CheckBox.new " ",self
cb.setChecked false
connect cb, SIGNAL("toggled(bool)"), self, SLOT("on_toggled(bool)")
#label.move 65,130
cb.move 170,130
end
def on_toggled state
if state
#lineedit = Qt::LineEdit.new(self)
#lineedit.setFont Qt::Font.new("Times New Roman", 12)
#label = Qt::Label.new "Address: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
#label.move 380,130
#label.resize 170,20
#lineedit.move 500,130
#lineedit1 = Qt::LineEdit.new(self)
#lineedit1.setFont Qt::Font.new("Times New Roman", 12)
#label1 = Qt::Label.new "Zip Code: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
#label.move 420,160
#label.resize 70,20
#lineedit.move 500,160
end
end
end
app = Qt::Application.new(ARGV)
widget = Auth.new
widget.show
app.exec
Can someone let me know what changes I should make so that, when I click the checkbox, I get the other required widgets?

Related

Dropdown Menu and its action

I have a dropdown menu with around 5-6 items in it.
require 'Qt'
class Auth < Qt::Widget
entryIndex = $entryIndex
slots 'slotFunctionChanged(int)'
def initialize(parent=nil)
super(parent)
setWindowTitle("Action");
setFixedSize 750,530
entry_ui
show
end
def entry_ui
#entryLabel = Qt::Label.new "Entry: ", self
#entryLabel.setFont Qt::Font.new("Times New Roman", 14)
combo = Qt::ComboBox.new self
combo.setFont Qt::Font.new("Times New Roman", 12 )
combo.addItem "1- Standard"
combo.addItem "2- Custom"
combo.addItem "3- Non-custom"
combo.addItem "4- Non-Standard"
connect combo, SIGNAL('activated(int)'), self, SLOT('slotEntryChanged(int)')
combo.resize 170,20
combo.move 170,100
#funLabel.move 95,100
end
def slotEntryChanged(entryIndex)
case entryIndex
when 0
#acc.show
when 1
#acc.hide
when 2
#acc.show
end
end
Now there is a button called Submit , which is connected to slot 'on_clicked_submit()'.
I want a dialog box to pop up only when entryIndex item to be either custom or non- custom. I tried the following code:
def on_clicked_submit
if $entryIndex == 1 || $entryIndex == 8
text = Qt::InputDialog.getText self, "Swipe", "Thank you"
end
end
But the if statement to check entryIndex was not working fine. Suggestions are welcome

Keyboard Shortcut doesn't work in ruby-gtk2 TextView

I'm practicing on a GUI based on Gtk and Ruby. I planned to make a little window that has a TextView and a button. When you input text in the TextView and press the button, it calls the Twitter gem's method and tweets to Twitter.
After that succeeded, I wanted to make a keyboard short-cut to do this: like, when you hit Ctrl+Enter it Tweets.
Here's part of the source code I have written. (Only around the GUI)
# require "gtk2"
# require "twitter"
## error classes and methods first
class Error_pop
def initialize(text)
#pop = Gtk::Dialog.new(
"Error",
nil,
Gtk::Dialog::MODAL,
[Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
)
#pop.default_response = Gtk::Dialog::RESPONSE_OK
#pop.set_size_request(400, 100)
#label = Gtk::Label.new("#{ text }")
#table = Gtk::Table.new(3, 1, false)
#table.attach_defaults(#label, 0, 1, 0, 2)
#pop.vbox.add(#table)
end
attr_accessor :pop, :table, :label
end
module Tueet
def tueet(text, account)
begin
account.rest_api.update(text) # calls gem: twitter
rescue => reason
error_pop = Error_pop.new(reason)
error_pop.pop.show_all
error_pop.pop.run do |response|
if response == Gtk::Dialog::RESPONSE_OK
end
end
error_pop.pop.destroy
return text
else
return nil
end
end
module_function :tueet
end
## making gui
window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.title = "Tweet Box"
window.set_size_request(300, 150)
window.resizable = false
window.signal_connect('delete_event') do
Gtk.main_quit
false
end
inputter = Gtk::TextView.new
inputter.editable = true
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
inputter_event = Gtk::EventBox.new # for key binding
inputter_event.events = Gdk::Event::KEY_PRESS_MASK
tweetbutton = Gtk::Button.new("tweet")
table = Gtk::Table.new(4, 9, true)
table.attach(inputter, 0, 9, 0, 3, Gtk::FILL||Gtk::EXPAND, Gtk::FILL||Gtk::EXPAND, 5, 5)
table.attach(tweetbutton, 3, 6, 3, 4)
inputter_event.add(table)
window.add(inputter_event)
inputter_event.realize
## signals and stuff
text = nil
tweetbutton.signal_connect('clicked') do
text = inputter.buffer.text
returned_text = Tueet.tueet(text, client[0]) # client[n] holds the authorized account information
inputter.buffer.text = String(returned_text)
end
inputter_event.signal_connect('key-press-event') do |wdt, evt|
key = Gdk::Keyval.to_name(evt.keyval)
p ("KeyPress: #{ key }was pressed") # these lines
p evt.state # are for
p evt.keyval # debugging
if key == 'Return' && # when "Ctrl + Return", do Tueet.tueet
( evt.state & Gdk::Window::CONTROL_MASK ==
Gdk::Window::ModifierType::CONTROL_MASK )
Tueet.tueet(text, client[0])
end
end
window.show_all
Gtk.main
And, this doesn't work. When I press Ctrl + Enter, it simply linebreaks inside the TextView.
Only, when doing this:
inputter = Gtk::TextView.new
inputter.editable = false # <- making this false
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
makes the keybinding work, but disables me to input what I want to tweet to the TextView.
How can I work this out? I wonder how many GUI applications do this.

error when creating hash in simple Credit Card class for ruby

I am creating a simple CC class that can create and update a credit card. To do this, I have created cc_bal{} as an instance object so it can update respect credit cards. The hash is to save and update a person and the amount on their cc. I end up getting an output of just the original amount that was created and not the updated amount
Heres the code:
class CC
def initialize(cc_name, cc_bal = {}, open_cc = false)
#cc_name = cc_name
#cc_bal = cc_bal
#open_cc = open_cc
end
def create(initAmount, person)
if initAmount > 0
#open_cc = true
#cc_bal[:person]=initAmount
puts "congrats #{person} on opening your new #{#cc_name} CC! with $#{#cc_bal[:person]}"
else
puts "sorry not enough funds"
end
end
def update(amount, person)
if #open_cc == true
#cc_bal[:person] + amount
else
puts "sorry no account created, #{person}"
end
puts "#{person} has CC balance of #{#cc_bal[:person]}"
end
end
#chase = Bank.new("JP Morgan Chase")
#wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
#chase.open_account(me)
#chase.open_account(friend1)
#wells_fargo.open_account(me)
#wells_fargo.open_account(friend1)
#chase.deposit(me, 200)
#chase.deposit(friend1, 300)
#chase.withdraw(me, 50)
#chase.transfer(me, wells_fargo, 100)
#chase.deposit(me, 5000)
#chase.withdraw(me, 5000)
#puts chase.total_cash_in_bank
#puts wells_fargo.total_cash_in_bank
credit_card = CC.new("Visa")
credit_card.create(10,me)
credit_card.update(50,me)
credit_card.create(20,friend1)
credit_card.update(40,friend1)
Please disregard the function calls that are commented out.
Any idea why the CC's are not updatiing?
if #open_cc == true
#cc_bal[:person] + amount
else
You increase the amount, but you don't set the new value anywhere. It should be
if #open_cc == true
#cc_bal[:person] += amount
else
Note. The code needs some serious refactoring and cleanup.

Signal timed ruby gtk image change without button_press_event, how to trigger looping

The line: pics.box.signal_connect("button_press_event"){pics.nuImage}, triggers nuImage and adds 1 to the picindex counter upon clicking, making the current image destroy, and next image show. I would like to make this automatic, like a slideshow without having to click. It needs to show a new image every x amount of seconds, using a sleep or something like GLib.timeout_add_seconds (), but I do not understand how to implement these options to continue looping without any user input. Thank you for your help, I am very new to ruby.
require 'gtk2'
class Pics
attr_accessor :pile, :picindex, :imgLoaded, :image, :box, :window, :time
def initialize
#window = Gtk::Window.new()
#window.signal_connect("destroy"){Gtk.main_quit}
pic1 = "1.jpg"
pic2 = "2.jpg"
pic3 = "3.jpg"
pic4 = "4.jpg"
#pile = [pic1, pic2, pic3, pic4]
#picindex = 0
self.getImage
#box = Gtk::EventBox.new.add(#image)
#time = true
end
def nuImage
#box.remove(#image)
#picindex = #picindex + 1
#picindex = 0 if #picindex == #pile.length
self.getImage
#box.add(#image)
#box.show
end
def getImage
#imgLoaded = #pile[#picindex]
img = Gdk::Pixbuf.new(#imgLoaded, 556, 900)
#image = Gtk::Image.new(img)
#image.show
end
end # class Pics
pics = Pics.new
pics.box.signal_connect("button_press_event"){pics.nuImage}
pics.window.set_default_size(556, 900)
pics.window.add(pics.box)
pics.window.show_all
Gtk.main
the following code is an implementation:
GLib::Timeout.add(1000) do
pics.nuImage if pics.time
true
end
pics.window.signal_connect("key_press_event") do |_window, event|
case event.keyval
when Gdk::Keyval::GDK_KEY_space
pics.time = !pics.time
end
end
more details: http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout
related: Ruby GTK Pixbuf timed image change

RubyMotion - Styling NSTableCellView with Teacup

I'm beginning with Rubymotion development, and I'm creating my first NSTableView with custom cells.
The following code is working, but I cannot manage to see how I should declare the NSTableCellView with Teacup layout (in order to move frame size in my stylesheet)
def tableView(table_view, viewForTableColumn: column, row: row_index)
cell_identifier = 'cell_id'
cell = table_view.makeViewWithIdentifier(cell_identifier, owner: self)
unless cell
cell = NSTableCellView.alloc.initWithFrame(CGRectMake(0, 0, table_view.bounds.size.width, rowHeight))
cell.identifier = cell_identifier
layout(cell) do
subview(NSTextField, :cell_text)
end
end
cell.subviews[0].stringValue = "value for #{row_index}"
cell
end
I already tried this code but it's not working:
def tableView(table_view, viewForTableColumn: column, row: row_index)
cell_identifier = 'cell_id'
#cell = table_view.makeViewWithIdentifier(cell_identifier, owner: self)
unless #cell
layout(nil, :root) do
#cell = subview(NSTableCellView, :cell_view) do |cell|
cell.identifier = cell_identifier
subview(NSTextField, :cell_text)
end
end
end
#cell.subviews[0].stringValue = "value for #{row_index}"
#cell
end
Thanks for your help
It should work if you assign the stylesheet and call reapply!
cell.stylesheet = :stylesheet
cell.reapply!

Resources