Golang TreeView example not working
-
Hello there,
I am new to Qt and I try to integrate a TableView in my application. The example from github gave me a compilation error, however I managed to solve the issue by creating a constructor for my TableModel. The number of elements displayed is good but the cells are empty.
On stackoverflow I found some solutions including changing the parent of the TableModel and implementing insertRows and DeleteRows(I have different names for that but it should be ok according to the documentation since I use BeginRemoveRows and EndRemoveRows).
Can anyone tell me what is wrong with my code?
//source: https://github.com/therecipe/examples/blob/master/advanced/widgets/tableview package main import ( "github.com/therecipe/qt/core" "github.com/therecipe/qt/widgets" "os" ) type TableItem struct { firstName string lastName string } type CustomTableModel struct { core.QAbstractItemModel //_ func() `constructor:"init"` // //_ func() `signal:"remove,auto"` //_ func(item TableItem) `signal:"add,auto"` //_ func(firstName string, lastName string) `signal:"edit,auto"` modelData []TableItem } func NewCustomTableModel(parent core.QObject_ITF) *CustomTableModel { m := &CustomTableModel{ QAbstractItemModel: *core.NewQAbstractItemModel(parent), modelData: []TableItem{}, } m.add(TableItem{"john", "doe"}) m.add(TableItem{"john", "bob"}) m.ConnectHeaderData(m.headerData) m.ConnectRowCount(m.rowCount) m.ConnectColumnCount(m.columnCount) m.ConnectData(m.data) //m.ConnectIn return m } func (m *CustomTableModel) headerData(section int, orientation core.Qt__Orientation, role int) *core.QVariant { if role != int(core.Qt__DisplayRole) || orientation == core.Qt__Vertical { return m.HeaderDataDefault(section, orientation, role) } switch section { case 0: return core.NewQVariant12("FirstName") case 1: return core.NewQVariant12("LastName") } return core.NewQVariant() } func (m *CustomTableModel) rowCount(*core.QModelIndex) int { return len(m.modelData) } func (m *CustomTableModel) columnCount(*core.QModelIndex) int { return 2 } func (m *CustomTableModel) data(index *core.QModelIndex, role int) *core.QVariant { println(role) println("data") if role != int(core.Qt__DisplayRole) { return core.NewQVariant() } item := m.modelData[index.Row()] switch index.Column() { case 0: return core.NewQVariant12(item.firstName) case 1: return core.NewQVariant12(item.lastName) } return core.NewQVariant() } func (m *CustomTableModel) remove() { if len(m.modelData) == 0 { return } m.BeginRemoveRows(core.NewQModelIndex(), len(m.modelData)-1, len(m.modelData)-1) m.modelData = m.modelData[:len(m.modelData)-1] m.EndRemoveRows() } func (m *CustomTableModel) add(item TableItem) { println("add") m.BeginInsertRows(core.NewQModelIndex(), len(m.modelData), len(m.modelData)) m.modelData = append(m.modelData, item) m.EndInsertRows() println(len(m.modelData)) for e := range m.modelData { println(m.modelData[e].lastName,m.modelData[e].firstName ) } } func (m *CustomTableModel) edit(firstName string, lastName string) { println("edit") if len(m.modelData) == 0 { return } m.modelData[len(m.modelData)-1] = TableItem{firstName, lastName} m.DataChanged(m.Index(len(m.modelData)-1, 0, core.NewQModelIndex()), m.Index(len(m.modelData)-1, 1, core.NewQModelIndex()), []int{int(core.Qt__DisplayRole)}) } func newTableView() *widgets.QWidget { widget := widgets.NewQWidget(nil, 0) widget.SetLayout(widgets.NewQVBoxLayout()) tableview := widgets.NewQTableView(nil) model := NewCustomTableModel(nil) tableview.SetModel(model) widget.Layout().AddWidget(tableview) remove := widgets.NewQPushButton2("remove last item", nil) remove.ConnectClicked(func(bool) { model.remove() }) widget.Layout().AddWidget(remove) add := widgets.NewQPushButton2("add new item", nil) add.ConnectClicked(func(bool) { model.add(TableItem{"john", "doe"}) }) widget.Layout().AddWidget(add) edit := widgets.NewQPushButton2("edit last item", nil) edit.ConnectClicked(func(bool) { model.edit("bob", "omb") }) widget.Layout().AddWidget(edit) return widget } func main() { app := widgets.NewQApplication(len(os.Args), os.Args) widget := newTableView() widget.SetWindowTitle("Tree view") widget.Show() app.Exec() }
-
Hi and welcome to the forums
I dont know Golang but the interface is the same so i would
check in func (m *CustomTableModel) data
that this test
if role != int(core.Qt__DisplayRole) {
return core.NewQVariant()
}is not always returning an empty variant for each call.
also
case 0:
return core.NewQVariant12(item.firstName)
case 1:
return core.NewQVariant12(item.lastName)does return a valid QVariant ?
i wondered why the empty one is
NewQVariant()
but with data its NewQVariant12 ?