QT 自定义tablemodel

       QAbstractTableModel提供了一个相对于QAbstractItemModel更便捷的方式来实现一个自定义的tablemodel。

     When subclassing QAbstractTableModel, you must implement rowCount(), columnCount(), and data(). Default implementations of the index() and parent() functions are provided by QAbstractTableModel. Well behaved models will also implement headerData().

     上面一段话摘自qt documents,从它可以看到,如果要实现一个自定义的tablemodel,你的实现一个派生类派生至qabstractTableModel,并重新实现rowCount和columnCount,其中前者用于表示当前table的行数,后者返回table的列数。data在界面需要更新的时候调用。并且如果要实现一个更好看的model,你还可以重新实现headerData函数。

      这里我主要介绍一下data函数,这个函数声明如下:

QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const

         其中index表示当前需要更新的位置,index包含了行号和列号。在Qt中role有很多种,针对每一个index,view会一次调用data来询问每一个role。

Qt::DisplayRole 0 The key data to be rendered in the form of text. (QString)
Qt::DecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColorQIcon or QPixmap)
Qt::EditRole 2 The data in a form suitable for editing in an editor. (QString)
Qt::ToolTipRole 3 The data displayed in the item's tooltip. (QString)
Qt::StatusTipRole 4 The data displayed in the status bar. (QString)
Qt::WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString)
Qt::SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize)

Roles describing appearance and meta data (with associated types):

Constant Value Description
Qt::FontRole 6 The font used for items rendered with the default delegate. (QFont)
Qt::TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt::AlignmentFlag)
Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
Qt::BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead.
Qt::ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
Qt::TextColorRole 9 This role is obsolete. Use ForegroundRole instead.
Qt::CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt::CheckState)
          我要说的是在这里,我们必须考虑的是DisplayRole和EditRole,如果没有实现EditRole,那么当你点击了一个可编辑单元但是没有输入任何值就确认的时候,这个单元将被设为默认,在这里我遇到的情况是我的单元格的类型是double,则如果我点击了该单元格而没有输入,则该单元格内容会变为0,我的估计是setData函数在没有输入的情况下会通过data函数的返回值来确定该单元格的值。

        接下来就是为了要实现可编辑,必须重新实现setData和flags函数。其中在flags函数中给相应的itme增加Qt::ItemIsEditable标记。

Qt::ItemFlags QAbstractItemModel::flags ( const QModelIndex & index ) const {

      Qt::ItemFlags flag = QAbstractTableModel::flags(index);

      flag |= Qt::ItemIsEditable;

      return flag;

}

bool TableModel::setData(const QModelIndex &index, const QVariant &v, int role) {
   if (index.isValid() && role == Qt::EditRole && v.isValid()) {
         /**copy v to which data that index show*/
    }
    return false;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值