我试图将User_Info类中的msg
var从show
方法传递到Call_Win类中的show
方法。因为我得到了rubyv2/call_win.rb:4:in initialize: unresolved constructor call Call_Win (ArgumentError)
,我会陷入困境。
main.rb的
require "Qt"
require "unirest"
require "redis"
class QtApp < Qt::Widget
require_relative "user_info"
slots "login()"
def initialize
super
setWindowTitle "Login"
init_ui
resize 400, 90
move 0, 0
show
end
def init_ui
@show = Qt::PushButton.new "Login", self
connect(@show, SIGNAL("clicked()"),
self, SLOT("login()"))
@show.move 20, 20
@username = Qt::LineEdit.new self
@username.move 130, 20
@username.setText "remy@gmail.com"
@password = Qt::LineEdit.new self
@password.setEchoMode(2)
@password.move 130, 50
@password.setText "remy@gmail.com"
end
def login
button = sender
if "Login" == button.text
call()
elsif "Logout" == button.text
logout()
end
end
def logout
@app.quit
end
def call
response = Unirest::post("http://localhost:3000/user_token",
headers:{
"content_type" => "application/json"
},
parameters:{auth: [{
:email => "#{@username.text}",
:password => "#{@password.text}"
}]}
)
$global_variable = "#{response.body["jwt"]}"
puts "#{response.code} #{@username.text} #{@password.text}"
if response.code == 201
@show.setText "Logout"
Qt::MessageBox.information self, "#{$global_variable}", " Logged In ;) [#{response.body["jwt"]}]"
User_Info.new
elsif response.code == 404
Qt::MessageBox.warning self, "#{@username.text}", "Unkown User"
end
end
end
@app = Qt::Application.new ARGV
QtApp.new
@app.exec
User_Info类
class User_Info < Qt::Widget
require_relative 'call_win'
def initialize
super
setWindowTitle "Menu"
init_ui
resize 400, 600
move 401, 0
show
end
def init_ui
$redis = Redis.new(host: "192.168.43.1", port: 6379)
show()
end
def show()
$redis.subscribe('ruby') do |on|
on.message do |channel, msg|
Call_Win.new("#{msg}")
end
end
end
end
Call_Win Class
class Call_Win < Qt::Widget
def initialize(message)
super
@msg = message
setWindowTitle "Menu"
init_ui
resize 400, 600
move 401, 0
show
end
def init_ui
puts @msg
show(@msg)
end
def show(msg)
Qt::MessageBox.information self, "#{msg}", "#{msg}"
end
end
答案 0 :(得分:1)
问题是由于通过调用Qt::Widget
传递给super
构造函数的错误数量的参数引起的:
class Call_Win < Qt::Widget
def initialize(message)
super # HERE
...
end
...
end
Qt::Widget#initialize
不接受参数。要在不带参数的情况下调用super
方法,应该明确地不向其传递任何参数:
class Call_Win < Qt::Widget
def initialize(message)
super() # HERE
在没有括号的情况下调用super
将所有给出的参数重新传递给祖先的函数。由于Call_Win
的构造函数接收单个参数,因此它将通过super
传递给祖先的构造函数。对super()
的显式调用不会将任何参数传递给Qt::Widget#initialize
,从而使继承按预期工作。