怎样实现qtableview或者qtablewidget的单元格自动换行

memorycake 2014-07-14 10:35:47
网上搜到有自动调整列高的方法,和qlable自动换行的方法,都不行。
大家有遇到过同样的问题吗?
...全文
2775 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
蜗牛sf 2014-07-15
  • 打赏
  • 举报
回复 1
void QTableView::resizeRowsToContents () [slot]

Resizes all rows based on the size hints of the delegate used to render each item in the rows.
试下这个,resizeEvent下调用这个,在填完数据后调用
yjzl1911 2014-07-15
  • 打赏
  • 举报
回复
tableview单元格换行的方法: (1)实现自己的delegate,重写 paint 函数,其中关键点是加上 Qt::TextWordWrap :
//重载绘制函数, 目的使得 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 );

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧