我正在使用Qt5.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);
}不幸的是,这个函数返回了一些奇怪的结果,使得字符串的填充变得很大:

有没有办法解决这个问题?
发布于 2014-05-08 17:40:54
所以,最终,我找到了我的代码中导致问题的那部分。这个字符串,
tempTable.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);我为了将列拉伸到页面宽度而设置的,不知何故以意想不到的方式影响了boundingRect的行为。在我删除它并使用setColumnWidth函数设置每一列的宽度后,一切看起来都很好。
https://stackoverflow.com/questions/23519070
复制相似问题