I've created a 'confirm exit' dialog box to prompt the user when exiting. I've successfully connected it to an 'exit' menu command, but I also want to connect it to the window-close (X) button. How can I do this? I've had some experience with Java Swing, and to accomplish this task you had to add a window listener to the frame that would call this prompt. Is there something similar I must do here?
Do it like this:
require 'fox16'
include Fox
class MyApp < FXMainWindow
def initialize(app)
#app = app
super(app, "Test", :height => 150, :width => 350, :opts=> DECOR_ALL)
self.connect(SEL_CLOSE, method(:on_close))
end
def create
super
show(PLACEMENT_SCREEN)
end
def on_close(sender, sel, event)
q = FXMessageBox.question(#app, MBOX_YES_NO, "Sure?", "You sure?")
if q == MBOX_CLICKED_YES
getApp().exit(0)
end
end
end
FXApp.new do |app|
MyApp.new(app)
app.create
app.run
end
Related
How to click an element in RecyclerView by using Appium, Ruby. I tried using all ways but not able to click
I have tried all possible ways
require 'selenium-cucumber'
require 'appium_lib'
def alert1
$driver.find_element(xpath: "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[2]")
end
def alert2; $driver.find_element(xpath: "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[2]") end
def phoneNumber; $driver.find_element(id: "com.accushield.familyapp:id/phone_number") end
def sendCode; $driver.find_element(id: "com.accushield.familyapp:id/send_code") end
def confirmationCode; $driver.find_element(id: "com.accushield.familyapp:id/confirmation_code") end
def submitCode; $driver.find_element(id: "com.accushield.familyapp:id/submit_confirmation_code") end
def dashboardScreen1; $driver.find_element(xpath: "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.support.v7.widget.RecyclerView/android.widget.FrameLayout[1]/android.view.ViewGroup").click end
Then(/^do user Login with OTP$/) do
alert1.click
alert2.click
phoneNumber.send_keys("8131234567")
sendCode.click
confirmationCode.send_keys("123456")
submitCode.click
sleep 40
#residentClick.click
#$driver.find_element(xpath: "//android.widget.ImageView[#index='1']").click
dashboardScreen1.click
#if dashboardScreen.displayed?
# puts "User has signed in successfully"
# else
# puts "User sign in got failed"
# end
I need to click any element in recycler view. Please find the image for elements
https://i.stack.imgur.com/cmJ23.png
I want to execute a user defined function after a button was pressed. I don't know how to use the connect function correctly to achieve the behavior specified in the code snippet.
#!/usr/bin/env ruby
require 'Qt4'
def do_sth
print "did something"
end
app = Qt::Application.new(ARGV)
btn = Qt::PushButton.new('Button')
btn.resize(75, 30)
btn.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
# A button click will close the application.
#Qt::Object.connect(btn, SIGNAL('clicked()'),app, SLOT('quit()'))
#
# FIXME How to execute the function do_sth if the button was pressed?
Qt::Object.connect(btn, SIGNAL('clicked()'),app, SLOT('do_sth()'))
btn.show()
app.exec()
Thanks for your hint, it worked the way you suggested.
#!/usr/bin/env ruby
require 'Qt4'
class Qo < Qt::Object
slots 'do_sth()'
slots 'bla()'
def do_sth
puts "did something"
end
def bla
puts "blabla"
end
end
qobj = Qo.new
app = Qt::Application.new(ARGV)
btn = Qt::PushButton.new('Button')
btn.resize(75, 30)
btn.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
Qt::Object.connect(btn, SIGNAL('clicked()'),qobj, SLOT('do_sth()'))
Qt::Object.connect(btn, SIGNAL('pressed()'),qobj, SLOT('bla()'))
btn.show()
app.exec()
I don't know if I'm getting the whole documentation totally wrong but there is an issue I'm dealing with since two days and I just don't get what I'm doing wrong. I'm using fxruby to built a small GUI, and I need a progressbar within that. When I initialize it with no parameters its just ridiculously small, so I tried to use the barSize option (wich is responsible for the width, at least the documentation says so). This is my source code:
require 'fox16'
require 'pp'
include Fox
class Test < FXMainWindow
def initialize(app)
super(app, "Test")
hFrame1 = FXHorizontalFrame.new(self)
#progBar = FXProgressBar.new(hFrame1)
pp #progBar.barSize
#progBar.barSize=100
#progBar.setTotal(10)
#progBar.setProgress(5)
pp #progBar.barSize
def create
super
show(PLACEMENT_SCREEN)
end
end
end
FXApp.new do |app|
Test.new(app)
app.create
app.run
end
But this is what it looks like:
]1
So obviously the height gets changed. Obviously I also tried things like
:width => 150
in the constructor but it did not work at all. Seems like I'm just to stupid for fxruby. Could anyone point me to the right way please?
You need to learn about layout parameters. Try this modification of your Test class and I think you will know where to go from there:
class Test < FXMainWindow
def initialize(app)
super(app, "Test", :width => 200, :height => 100)
hFrame1 = FXHorizontalFrame.new(self, LAYOUT_FILL)
#progBar = FXProgressBar.new(hFrame1, :opts => LAYOUT_FILL_X)
pp #progBar.barSize
#progBar.barSize=10
#progBar.setTotal(10)
#progBar.setProgress(5)
pp #progBar.barSize
def create
super
show(PLACEMENT_SCREEN)
end
end
end
As far as I know, if all I wanted to do was do "puts" in a console, then I would not be having to ask this question. (However, finally I am asking you all at StackOverflow myself, though I've been visiting for years.)
Here is my issue:
I am trying to create a variable which will be "set" to a specific value upon user click
I am then trying to display that value after it is changed
I can set the value, but it does not get displayed
(Of course, this should work if I am not using Shoes.)
Here is the relevant portion of my code:
class Inits
# Declares all global vars used
def init1
...
end
def initattrib
#id = "###"
#name = "name undef"
alert("Alert: default attrib set")
end
def id
#id
#alert("Alert: id call")
end
def name
#name
#alert("Alert: name call")
end
def id=(newid)
#id = newid
#alert("Alert: id set")
end
def name=(newname)
#name = newname
#alert("Alert: name set")
end
end
Then I am trying to call the id and set it as so:
Shoes.app :width => 800, :height => 600, :resizable => false do
currclass = Inits.new
currclass.init1
currclass.initattrib
.
.
.
id = "123"
name = "new name"
# I declare something to click here
click { currclass.id = id, currclass.name = name }
# Then I try to display it as so:
para currclass.id
para currclass.name
# But of course the value is not displayed -- just the default value
end
... As an aside, I am pretty sure I should be using instance variables and not class variables (#x, not ##x).
Is there some way I can "update on change" ("clock rising edge" is a good analogy) or some other way to call this?
Anyhow, thank you in advance for any advice on what I am not doing correctly. Perhaps there is a misunderstanding.
The first thing you learn using Shoes: It is a metaprogramming jungle.
Shoes has hooks to default procedures, and I'm sure it has one for Class creation as well so Shoes can add its own wizardry to that Class being specified.
Which would mean that a Class - defined outside of Shoes.app - might now work the same as if defined inside Shoes.app.
Try moving everything inside Shoes.app block, I had a lot of problems with pieces of code laying outside of Shoes.app (usually scope problems).
If I understood correctly what you want to do you should do it like that:
class Cos
attr_accessor :co
def initialize(cos)
#co=cos
end
end
Shoes.app :width => 800, :height => 600, :resizable => false do
#cosik = Cos.new("ddd")
#ap=para(#cosik.co)
button "click" do
#cosik.co = "oj"
#ap.text=#cosik.co
end
end
K
[EDIT NOTE: Noticed I had put the mutex creation in the constructor. Moved it and noticed no change.]
[EDIT NOTE 2: I changed the call to app.exec in a trial run to
while TRUE do
app.processEvents()
puts '."
end
I noticed that once the Soap4r service started running no process events ever got called again]
[EDIT NOTE 3: Created an associated question here: Thread lockup in ruby with Soap4r
I'm attempting to write a ruby program that receives SOAP commands to draw on a monitor (thus allowing remote monitor access). I've put together a simple test app to prototype the idea. The graphic toolkit is QT. I'm having what I assume is a problem with locking. I've added calls to test the methods in the server in the code shown. The server side that I'm testing right now is:
require 'rubygems'
require 'Qt4'
require 'thread'
require 'soap/rpc/standaloneserver'
class Box < Qt::Widget
def initialize(parent = nil)
super
setPalette(Qt::Palette.new(Qt::Color.new(250,0,0)))
setAutoFillBackground(true)
show
end
end
class SOAPServer < SOAP::RPC::StandaloneServer
##mutex = Mutex.new
def initialize(* args)
super
# Exposed methods
add_method(self, 'createWindow', 'x', 'y', 'width', 'length')
end
def createWindow(x, y, width, length)
puts 'received call'
windowID = 0
puts #boxList.length
puts #parent
##mutex.synchronize do
puts 'in lock'
box = Box.new(#parent)
box.setGeometry(x, y, width, length)
windowID = #boxList.push(box).length
print "This:", windowID, "--\n"
end
puts 'out lock'
return windowID
end
def postInitialize (parent)
#parent = parent
#boxList = Array.new
end
end
windowSizeX = 400
windowSizeY = 300
app = Qt::Application.new(ARGV)
mainwindow = Qt::MainWindow.new
mainwindow.resize(windowSizeX, windowSizeY)
mainwindow.show
puts 'Attempting server start'
myServer = SOAPServer.new('monitorservice', 'urn:ruby:MonitorService', 'localhost', 4004)
myServer.postInitialize(mainwindow)
Thread.new do
puts 'Starting?'
myServer.start
puts 'Started?'
end
Thread.new do
myServer.createWindow(10,0,10,10)
myServer.createWindow(10,30,10,10)
myServer.createWindow(10,60,10,10)
myServer.createWindow(10,90,10,10)
end
myServer.createWindow(10,10,10,10)
Thread.new do
app.exec
end
gets
Now when I run this I get the following output:
Attempting server start
Starting?
received call
0
#<Qt::MainWindow:0x60fea28>
in lock
received call
0
#<Qt::MainWindow:0x60fea28>
This:1--
in lock
This:2--
out lock
At that point I hang rather than recieving the total of five additions I expect. Qt does display the squares defined by "createWindow(10,0,10,10)" and "createWindow(10,10,10,10)". Given that "This:1--" and "This:2--" show within a nexted in/out lock pair I'm assuming I'm using mutex horribly wrong. This is my first time with threading in Ruby.