1 根據(jù)數(shù)據(jù)內(nèi)容設(shè)定寬
resizeColumnToContents, resizeColumnsToContents
2 去掉網(wǎng)格 setShowGrid
3 委托
需要在單元格里進(jìn)行特別處理,如需要QLineEdit, QComcoBox等時,需要用委托機(jī)制來實現(xiàn)。
委托需要實現(xiàn)的幾個函數(shù)
QTableView, QTableWidget對其數(shù)據(jù)進(jìn)行委托:setItemDelegate, setItemDelegateForColumn, setItemDelegateForRow
委托時QItemDelegate需要重新實現(xiàn)的函數(shù):createEditor(創(chuàng)建控件),setEditorData(設(shè)置值),setModelData,updateEditorGeometry(設(shè)置大小)
部分實現(xiàn)代碼示例
QWidget *LLineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const


{
QLineEdit *editor = new CompleteLineEdit(parent);
return editor;
}

void LLineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const


{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit *>(editor);
lineEdit->setText(value);
}

void LLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const


{
QLineEdit *lineEdit = static_cast<QLineEdit *>(editor);
QString value = lineEdit->text();
model->setData(index, value, Qt::EditRole);
}

void LLineEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const


{
editor->setGeometry(option.rect);
}
