Inserting Items into Qt :: TreeWidget
Using QtRuby (via qtbindings ) I am trying to add items to QTreeWidget
. It says it has a method insertTopLevelItems()
, but it doesn't respond to it:
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)
How do I add items to this widget?
Ruby 2.0.0p353; Qt 4.8.6; OS X 10.9.5
source to share
You get method missing
because your arguments are of the wrong type. Unlike Ruby, C ++ must conform to arguments and result types, and a qtruby wrapper.
When called, insertTopLevelItems
you are missing the index argument and you have to build Qt::TreeWidgetItem
from each line. If the tree is empty, addTopLevelItem(...
does the same asinsertTopLevelItem(0,...
Here's some sample code to try:
(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
or
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
source to share