关于这个,总结一下有两种比较好的方式:
1、自定义QItemDelegate,实现paint函数来对内容换行,实现sizeHint函数来调整行高(未实际测试)
主要内容:
void MyWrapTextDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex & index ) const
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
//绘制文本
painter->drawText( option.rect, Qt::TextWordWrap | Qt::AlignVCenter, text );
//如果当前有焦点,就绘制一个焦点矩形,否则什么都不做
drawFocus(painter, option, option.rect);
}2、一行代码搞定(数据量过多时可能有刷新效率问题)
//手动调整列宽时根据内容变化行高
connect(ui->sheetList_TableView->horizontalHeader(),SIGNAL(sectionResized(int, int, int)),
ui->sheetList_TableView,SLOT(resizeRowsToContents()));
//初始化及插入新行时主动刷新行高
connect(ui->sheetList_TableView->model(),SIGNAL(rowsInserted(QModelIndex,int,int)),
this,SLOT(on_TableViewColWidth_changed(QModelIndex,int,int)));
//槽函数---自动换行
void DispatchForm::on_TableViewColWidth_changed(const QModelIndex &index, int first, int last)
{
ui->sheetList_TableView->resizeRowToContents(first);
}3、其他注意:
TableView->setTextElideMode(Qt::TextElideMode model)为文字超出显示范围时 ... 出现的模式,有如下几种Constant ValueDescription
Qt::ElideLeft 0The ellipsis should appear at the beginning of the text.
Qt::ElideRight 1The ellipsis should appear at the end of the text.
Qt::ElideMiddle 2The ellipsis should appear in the middle of the text.
Qt::ElideNone 3Ellipsis should NOT appear in the text.
2598

被折叠的 条评论
为什么被折叠?



