QAbstractTableModel implementation fails after short usage
-
I'm trying to implement a QTableView using the QAbstractTableModel. I have written some code which seems to work ok (it opens three windows each showing a table with data), but if you use the windows for a few seconds (select cols/rows or try to resize the windows) it crashes with the error "Abstract method rowCount not implemented! In =(tail call)" - sometimes it also misses another method like data.
Unfortunately I use lqt, a lua implementation of Qt. I hope that you can still understand the code. Can you imagine what goes wrong here? Would you think that my implementation is missing something or do you think that lqt does something strange here?
@require'qtcore'
require'qtgui'app = QApplication(1 + select('#', ...), {arg[0], ...})
local TableView = {}
for n = 1,3 do -- open three windows
local mytable = {} -- create 2D data table
for i = 1,10 do
mytable [i] = {}
for j = 1,20 do
mytable [i][j] = n
end
endTableView[n] = QTableView.new()
local DataModel = QAbstractTableModel.new(TableView[n])
local parent = QModelIndex()function DataModel:rowCount() return #mytable[1] end -- return number of rows in first column
function DataModel:columnCount() return #mytable end -- return number of columns
function DataModel:parent() return parent endlocal nothing = QVariant()
function DataModel:data(index, role)
if role == Qt.ItemDataRole.DisplayRole then -- DisplayRole = 0
local row = index:row()
local col = index:column()
return QVariant(mytable [col+1][row+1]) -- get the data from your Lua table
else
return nothing
end
endTableView[n]:setModel(DataModel)
TableView[n]:show()end -- for n
app.exec()@
-
Hi,
As I know, QAbstractTableModel is abstract So to use it we have to subclass it.
When subclassing QAbstractTableModel, you must implement the three pure virtual methods :@
virtual int rowCount ( const QModelIndex & parent = QModelIndex() )
virtual int columnCount ( const QModelIndex & parent = QModelIndex() )
virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole )
@They are pure virtual (const=0) so they have to be reimplemented !!!