使用QtRuby(通过qtbindings)我正在尝试将项目添加到QTreeWidget
。它说它有insertTopLevelItems()
方法,但它无法响应它:
hier = $my.appHierarchy
hier.column_count = 2
hier.header_labels = ['element', 'kind']
p hier.class, hier.methods.grep(/insert/)
#=> Qt::TreeWidget
#=> ["insertAction", "insertActions", "insertTopLevelItem", "insertTopLevelItems"]
hier.insertTopLevelItems ['x','y']
#=> in `method_missing': undefined method `insertTopLevelItems' for #<Qt::TreeWidget:0x007fc6c9153528> (NoMethodError)
如何向此窗口小部件添加项目?
Ruby 2.0.0p353; Qt 4.8.6; OS X 10.9.5
答案 0 :(得分:1)
您收到method missing
,因为您的参数类型错误。与Ruby不同,C ++需要匹配参数和结果类型,qtruby包装器也是如此。
调用insertTopLevelItems
时,您缺少索引参数,并且必须从每个字符串构建Qt::TreeWidgetItem
。如果树为空,addTopLevelItem(...
与insertTopLevelItem(0,...
以下是一些尝试的示例代码:
(1..10).each do |n|
item = Qt::TreeWidgetItem.new
item.setText(0, "item #{n}/1")
item.setText(1, "item #{n}/2")
hier.insertTopLevelItem(0, item)
# hier.addTopLevelItem(item) # same effect as previous line
end
或
itemlist = (1..10).collect do |n|
item = Qt::TreeWidgetItem.new
item.setText(0, "item #{n}/1")
item.setText(1, "item #{n}/2")
item
end
hier.insertTopLevelItems(0, itemlist)
# hier.addTopLevelItems(itemlist) # same effect as previous line