Widget should show or hide based on option chosen in drop down menu

时间:2016-02-12 20:13:00

标签: ruby qt methods qtruby

I have a dropdown menu with around 5-6 items in it.

I want other widgets to appear in the same window when I select the particular option in the ComboBox. For example: when I chose "1-Standard" in the ComboBox, the widget defined in acc_ui has to pop up and so on.

This is the code I tried:

require 'Qt'
class Auth < Qt::Widget

  slots 'slotFunctionChanged(int)'

  def initialize(parent=nil)
    super(parent)
    setWindowTitle("Action");
    setFixedSize 750,530

    function_ui

    show   
  end

  def function_ui
    @funLabel = Qt::Label.new "Func: ", self 
    @funLabel.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"
    combo.addItem "5- Plastic"

    connect combo, SIGNAL('activated(int)'), self, SLOT('slotFunctionChanged(int)')
    combo.resize 170,20
    combo.move 170,100
    @funLabel.move 95,100

  end 

  def slotFunctionChanged(index)
    case index 
    when 0 
      acc_ui()
    when 1 
      store_ui()     
    end 
  end 

  def acc_ui 
    @accLineedit = Qt::Lineedit.new(self)
    @accLineedit.setFont Qt::Font.new("Times New Roman", 12)
    @accLabel = Qt::Label.new "Acc: ", self 
    @accLabel.setFont Qt::Font.new("Times New Roman", 14)
    @accLabel.move 95,185
    @accLineedit.resize 170,20
    @accLineedit.move 170,185
  end 

  def store_ui
    @storeLineedit = Qt::Lineedit.new(self)
    @storeLineedit.setFont Qt::Font.new("Times New Roman", 12)
    @storeLabel = Qt::Label.new "Store: ", self 
    @storeLabel.setFont Qt::Font.new("Times New Roman", 14)
    @storeLabel.move 120,210
    @storeLineedit.resize 140,20
    @storeLineedit.move 170,210

  end 

end 

app = Qt::Application.new(ARGV)
widget = Auth.new
widget.show
app.exec

1 个答案:

答案 0 :(得分:0)

  • 将所有代码从acc_ui移至store_ui移至function_ui
  • {li} in function_ui:将show方法应用于默认情况下应显示的所有小部件,并将hide方法应用于默认情况下应隐藏的所有小部件 {li} in acc_uistore_ui:仅使用showhide方法让窗口小部件按照您的意愿显示和消失