在访问类变量(Qt::Action
s的数组)时,我遇到了分段错误的问题,我一直试图将其简化为复制问题的最小代码示例:
#!/usr/bin/ruby
require 'Qt4'
require 'rufus/scheduler'
app = Qt::Application.new(ARGV)
class ManagerWidget < Qt::Widget
signals 'changed(int)'
def initialize
super(nil)
max_index = 2
next_index = 0
scheduler = Rufus::Scheduler.start_new
scheduler.every '10s' do
emit changed(next_index)
if next_index < max_index - 1
next_index += 1
else
next_index = 0
end
end
end
end
class Tray < Qt::Widget
def initialize
super(nil)
tray = Qt::SystemTrayIcon.new
manager_widget = ManagerWidget.new
menu = Qt::Menu.new
tray_icon = Qt::Icon.new("./icons/Gnome-Preferences-Desktop-Wallpaper-64.png")
actions = []
manager_widget.connect(SIGNAL('changed(int)')) do |i|
puts "changed #{i}"
actions[i].text = '...' if actions && actions[i]
end
2.times do |i|
sub_menu = Qt::Menu.new("#{i + 1}")
actions[i] = sub_menu.add_action('x')
menu.add_menu(sub_menu)
end
tray.icon = tray_icon
tray.context_menu = menu
tray.show
end
end
Tray.new
app.exec
以上代码输出:
"sni-qt/12147" WARN 14:35:48.326 void StatusNotifierItemFactory::connectToSnw() Invalid interface to SNW_SERVICE
changed 0
changed 1
changed 0
changed 1
changed 0
changed 1
changed 0
changed 1
./min-code-example.rb:42: [BUG] Segmentation fault
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
[... snipped due to character limits on posting ...]
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
Aborted (core dumped)
Failed (134)
似乎有必要使用子菜单来触发这个问题 - 至少将操作添加到托盘菜单本身会运行很长一段时间没有问题(我把它留了10倍以上的时间)用以上代码获取错误。)
Ruby版本:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Qt版本:4.8.1
绑定版本:qtbindings (4.8.3.0)
Linux版本:Ubuntu 12.04
另一个奇怪的行为是puts
调用的输出有时会立即显示在控制台中,有时只会在鼠标悬停在托盘图标上时打印。
我甚至不确定在跟踪此问题时从哪里开始,所以您的任何输入都非常受欢迎。
更新:我已经离开了一段时间,因为我无法继续下去。我试图从这开始,但我仍然遇到同样的问题。我已经深入研究了这个问题,因为它说'missing_method'我开始打印类以确保我肯定在看Qt :: Action(我是),然后根据可用的方法actions[0].methods
。
.methods
输出:
[:setShortcut, :shortcut=, :qobject_cast, :inspect, :pretty_print, :className,
:class_name, :inherits, :findChildren, :find_children, :findChild, :find_child,
:connect, :method_missing, :const_missing, :dispose, :isDisposed, :disposed?,
:qVariantValue, :qVariantFromValue, :**, :+, :~, :-@, :-, :*, :/, :%, :>>,
:<<, :&, :^, :|, :<, :<=, :>, :>=, :==, :is_a?, :kind_of?, :methods,
:protected_methods, :public_methods, :singleton_methods, :qDebug, :qFatal,
:qWarning, :SIGNAL, :SLOT, :emit, :QT_TR_NOOP, :QT_TRANSLATE_NOOP, :nil?, :===,
:=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup,
:initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust,
:untrusted?, :trust, :freeze, :frozen?, :to_s, :private_methods,
:instance_variables, :instance_variable_get, :instance_variable_set,
:instance_variable_defined?, :instance_of?, :tap, :send, :public_send,
:respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method,
:define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=,
:instance_eval, :instance_exec, :__send__, :__id__, "blockSignals", "children",
"connect", "deleteLater", "disconnect", "dumpObjectInfo", "dumpObjectTree",
"dynamicPropertyNames", "event", "eventFilter", "inherits", "installEventFilter",
"widgetType?", "killTimer", "metaObject", "moveToThread", "objectName", "parent",
"property", "qt_metacall", "qt_metacast", "removeEventFilter", "objectName=",
"parent=", "setProperty", "setUserData", "signalsBlocked", "startTimer",
"thread", "userData", "actionGroup", "activate", "associatedGraphicsWidgets",
"associatedWidgets", "autoRepeat", "data", "font", "hover", "icon", "iconText",
"checkable?", "checked?", "enabled?", "iconVisibleInMenu?", "separator?",
"visible?", "menu", "menuRole", "parentWidget", "priority", "actionGroup=",
"autoRepeat=", "checkable=", "checked=", "data=", "disabled=", "enabled=",
"font=", "icon=", "iconText=", "iconVisibleInMenu=", "menu=", "menuRole=",
"priority=", "separator=", "shortcut=", "shortcutContext=", "shortcuts=",
"softKeyRole=", "statusTip=", "text=", "toolTip=", "visible=", "whatsThis=",
"shortcut", "shortcutContext", "shortcuts", "showStatusText", "softKeyRole",
"statusTip", "text", "toggle", "toolTip", "trigger", "whatsThis"]
在这种情况下,我在发生分段错误时尝试actions[0].enabled = true
,据我所知,它在输出中(从底部开始的第6行,enabled=
)。我还尝试了setEnabled(true)
,set_enabled(true)
以及我能想到的任何事情。
即使在操作对象上调用inspect
也会导致分段错误,但在最初将操作置于数组中的循环内执行此操作很好。我真的不知道这出错了。
编辑:为了回应使用QTimer
的建议,我尝试了这个,我仍然遇到同样的问题。代码目前比上面列出的要复杂得多,所以我将在这里写一些快速伪代码来说明流程:
def init_tray
actions = []
2.times do |i|
actions[i] = ... # init and add to systray, store reference in array
end
update = Proc.new do |i|
actions[i].setText('...') # update the text on one of the actions defined above
end
listener = ... # create a listener that can be called by Manager on certain events - will call the update Proc among other things.
manager = Manager.new(..., listener)
end
对Manager.new的调用初始化该对象,并在最后调用侦听器进行状态更改,然后调用访问update
数组的actions
Proc。在这个阶段,这应该都在同一个线程中,除了实际创建Qt::Action
之外,这些行中没有一个依赖于QT。我已经删除了Rufus调度并将其替换为QTimer
,但它还没有达到足够的程度。
答案 0 :(得分:1)
我没有看到你尝试过的一件事是使用更新版的ruby。 p0似乎可能充满了问题。
Ruby 1.9.3-p392是1.9.3的最新版本。
答案 1 :(得分:0)
我花了太长时间没有弄清楚为什么会发生这种情况(感谢那些试图帮助的人,但遗憾的是没有解决问题)。因此,我将我的解决方案作为答案,以防其他人遇到类似的问题:
我已经废除了数组,而是在我在先前填充数组的循环中创建的Proc
中包装要使用它的代码。由于在此循环中创建的Proc
可以直接访问相关变量,因此不再需要将其存储在数组中并在以后检索它。它提出了一两个问题,因为Proc
中的代码现在每个SIGNAL
执行一次,而不是让一个代码块执行所有相关操作,但它们更容易处理