1 根據數據內容設定寬
resizeColumnToContents, resizeColumnsToContents
2 去掉網格 setShowGrid
3 委托
需要在單元格里進行特別處理,如需要QLineEdit, QComcoBox等時,需要用委托機制來實現。
委托需要實現的幾個函數
QTableView, QTableWidget對其數據進行委托:setItemDelegate, setItemDelegateForColumn, setItemDelegateForRow
委托時QItemDelegate需要重新實現的函數:createEditor(創建控件),setEditorData(設置值),setModelData,updateEditorGeometry(設置大小)
部分實現代碼示例
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);
}
