2

Using QtRuby (via qtbindings) I am trying to add items to a QTreeWidget. It says that it has a insertTopLevelItems() method, but it fails to 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

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • One workaround is to insert the items during their creation directly, like so: `Qt::TreeWidgetItem.new(hier){ set_text 0, "My Label" }` – Phrogz Oct 06 '14 at 00:54

1 Answers1

1

You receive method missing because your arguments have the wrong type. Different from Ruby, C++  needs to match argument and result types, and so does the qtruby wrapper.

When calling insertTopLevelItems you are missing the index argument, and you must build a Qt::TreeWidgetItem from each string. If the tree is empty, addTopLevelItem(... does the same as insertTopLevelItem(0,...

Here is some example 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
bogl
  • 1,864
  • 1
  • 15
  • 32