Search code examples
rubyqtqtruby

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


I'm trying to pass msgvar from show method in User_Info Class to show method in Call_Win Class. I'am getting stuck as I'm getting 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 "[email protected]"
    @password = Qt::LineEdit.new self
    @password.setEchoMode(2)
    @password.move 130, 50
    @password.setText "[email protected]"
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

Solution

  • The issue is induced by wrong amount of arguments passed to Qt::Widget constructor via call to super:

    class Call_Win < Qt::Widget
      def initialize(message)
        super # HERE
        ...
      end
      ...
    end
    

    Qt::Widget#initialize does not accept arguments. To call the super method without arguments one should explicitly pass no arguments to it:

    class Call_Win < Qt::Widget
      def initialize(message)
        super() # HERE
    

    Call to super without parentheses re-passes all the arguments given to the ancestor’s function. Since the constructor of Call_Win receives the single argument, it’s being passed to the ancestor’s constructors via super. The explicit call to super() would pass no arguments to Qt::Widget#initialize, making the inheritance work as expected.