Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractTableModel implementation fails after short usage
QtWS25 Last Chance

QAbstractTableModel implementation fails after short usage

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.2k 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.
  • L Offline
    L Offline
    luafox
    wrote on last edited by
    #1

    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
    end

    TableView[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 end

    local 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
    end

    TableView[n]:setModel(DataModel)
    TableView[n]:show()

    end -- for n

    app.exec()@

    1 Reply Last reply
    0
    • I Offline
      I Offline
      issam
      wrote on last edited by
      #2

      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 !!!

      http://www.iissam.com/

      1 Reply Last reply
      0

      • Login

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