[英]QT FontMetrics::BoundingRect wrong size
我正在使用Qt 5.2,并且当前正在尝试从QTableView
打印表,但是遇到了按其内容计算行高的问题。 我现在得到的是下面的循环,该循环遍历QTableView
行,并使用boundingRect
函数获取每一行的高度。
for(int r=0; r<rows; ++r) {
//tempTable.resizeRowToContents(r);
QString str = tempTable.model()->data(tempTable.model()->index(r,0)).toString();
QFontMetrics fm(tempTable.font());
QRect rect = fm.boundingRect(0,0,tempTable.columnWidth(0),0,(Qt::TextWordWrap),str);
tempTable.setRowHeight(r,rect.size().height());
totalHeight += tempTable.rowHeight(r);
}
不幸的是,这个函数返回了一些奇怪的结果,给字符串增加了很大的填充空间:
有什么办法解决这个问题?
因此,最后,我找到了引起麻烦的代码部分。 这个字符串,
tempTable.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
我为了将列拉伸到页面宽度而放置的内容,以某种意外的方式影响了boundingRect
的行为。 在删除它并使用setColumnWidth
函数设置每列的宽度后,一切看起来都不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.