1 设置表头
QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
用法:
QVariant DockMessageModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
/*return QString("ABC %1").arg(section);*/
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
else
return QString("Row %1").arg(section);
}
2 其它设置,
this->setSelectionMode(QAbstractItemView::SingleSelection); // 只能选择一行
this->setSelectionBehavior(QAbstractItemView::SelectRows); //按行选
this->horizontalHeader()->setVisible(true); // 表头可见
this->horizontalHeader()->setStretchLastSection(true); // QTableview的函数,设置最后一列填充
this->verticalHeader()->setVisible(false);
3 获取edit后的表格或treeview的值
void QAbstractItemView::closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint)
Closes the given editor, and releases it. The hint is used to specify how the view should respond to the end of the editing operation. For example, the hint may indicate that the next item in the view should be opened for editing.