Ruby Qt 绑定:如何将变量传递给另一个 window?

Ruby Qt-bindings: How to pass variable to another window?

我正在尝试将 msgvar 从 User_Info Class 中的 show 方法传递到 Call_Win show 中的方法 Class。当我得到 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

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

问题是由通过调用 super 传递给 Qt::Widget 构造函数的参数数量错误引起的:

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 传递任何参数,从而使继承按预期工作。