Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Golang TreeView example not working
Forum Update on Monday, May 27th 2025

Golang TreeView example not working

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 975 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    razm
    wrote on 4 Aug 2019, 18:18 last edited by
    #1

    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()
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 Aug 2019, 19:45 last edited by
      #2

      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 ?

      1 Reply Last reply
      0

      1/2

      4 Aug 2019, 18:18

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved