16,216
社区成员
发帖
与我相关
我的任务
分享
void QTableView::resizeRowsToContents () [slot]
Resizes all rows based on the size hints of the delegate used to render each item in the rows.
试下这个,resizeEvent下调用这个,在填完数据后调用//重载绘制函数, 目的使得 item 中的文本可以自动换行
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
//绘制文本
painter->drawText( option.rect, Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter, text );
}
(2) 使用 tableview 相关的接口,根据表格的字数长度来设置表格的列高度,比如:
int rowHeight = (list.at(col).length() - 1) / DEFAULT_COL_WIDTH;
rowHeight += 1;
tv_channel->setRowHeight(row, rowHeight * DEFAULT_ROW_HEIGHT );