【问题标题】:Widget should show or hide based on option chosen in drop down menu小部件应根据下拉菜单中选择的选项显示或隐藏
【发布时间】:2016-02-12 20:12:59
【问题描述】:

我有一个包含大约 5-6 个项目的下拉菜单。

当我在 ComboBox 中选择特定选项时,我希望其他小部件出现在同一窗口中。例如:当我在 ComboBox 中选择“1-Standard”时,acc_ui 中定义的小部件必须弹出等等。

这是我试过的代码:

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

【问题讨论】:

  • 有什么问题?我们需要描述它没有正确执行的操作。请阅读“How to Ask”。此外,这是演示问题所需的最少代码吗?如果没有,请减少它。大量代码会减慢我们帮助您的能力,并占用我们帮助他人的时间。 minimal reproducible example.
  • 对不起。这是我第一次发帖,下次会记住这一点。我的问题是:在 case 语句中,当我尝试调用我单独定义的方法时,它没有被调用。

标签: ruby qt methods qtruby


【解决方案1】:
  • 将所有代码从acc_uistore_ui 移动到function_ui
  • function_ui 中:将show 方法应用于所有应默认显示的小部件,将hide 方法应用于所有应默认隐藏的小部件
  • acc_uistore_ui 中:只使用showhide 方法让小部件随心所欲地出现和消失

【讨论】:

  • 如果我想显示 storeLabel 小部件,我该如何显示它?显示和隐藏方法似乎不适用于此
  • @storeLabel.show 应该可以。如果没有,您可能需要将最后一个代码上传到 gist.github.com 并分享链接。我很乐意在那里检查并帮助您使其运行。
  • 谢谢。上述显示和隐藏的方式确实对我有用。但是,在处理个别案例之前,我不知道如何设置默认外观。
  • GUI 工作正常。但它似乎在特定屏幕上几分钟后因某种原因冻结。关于可能是什么原因的任何想法?
  • 不看代码怎么知道?正如已经提供的那样:如果您愿意,请输入gist.github.com,我会看看。
猜你喜欢
  • 2014-03-02
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多