2

I've been trying to add a simple combobox with four items to a stupid table widget cell and I still can't get it placed in the correct row and column. The best I could get was to make it show up only if I tell the combobox its parent is the table widget, the problem is it gets located at position x 0 y 0 of the table widget. I use setCellWidget method and it still doesn't populate the correct cell no matter what row and column I specify there. What should I do then?

I've found this example in PyQt but whenever I try to implement a ruby-esque version of it on Ruby it just doesn't work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Could you post your code, please? – kittykittybangbang Aug 11 '15 at 02:30
  • Honestly, my code should be quite crappy. If any one can post a short but working example of how to add a combobox to a table widget (Qt::TableWidget) I would really appreciate it. Let's say I want it to be inserted in row 1 column 3. –  Aug 11 '15 at 02:35

1 Answers1

0

This code does what you were looking for: generate a n×m table, and insert a combobox at a given cell, here at row 4 / column 2.

require 'Qt4'

qt_app = Qt::Application.new(ARGV)
win    = Qt::Dialog.new
win.show

table_widget   = Qt::TableWidget.new(win)
layout         = Qt::GridLayout.new(win)    # not required
layout.addWidget(table_widget)              # not required

rows    = table_widget.rowCount    = 7
columns = table_widget.columnCount = 4
(0..rows-1).each do |row|
  (0..columns-1).each do |column|
    text = "cell #{row}/#{column}"
    table_widget.setItem(row,column, Qt::TableWidgetItem.new(text))
  end
end

combo_box = Qt::ComboBox.new(table_widget)
combo_box.insertItems(0, ["one", "two", "three", "four", "five"])

table_widget.setCellWidget(4, 2, combo_box)
table_widget.show

qt_app.exec
bogl
  • 1,864
  • 1
  • 15
  • 32