Why does QPainter::boundingRect returns 0 0?
-
Here's what I've in the window:
and here's the code which generates that:
Window::Window(QWidget *parent) : QWidget(parent){ printer = new QPrinter(); printer->setPageSize(QPageSize::A4); printer->setPageMargins(QMarginsF(20,20,20,20), QPageLayout::Millimeter); document = new QTextDocument(this); auto rect = printer->pageRect(QPrinter::Point).size(); //qDebug() << rect.width() << rect.height(); document->setPageSize(QSizeF(rect.width(), rect.height())); createDocument(); auto preview = new QPrintPreviewWidget(printer); preview->setZoomMode(QPrintPreviewWidget::ZoomMode::FitToWidth); connect(preview, &QPrintPreviewWidget::paintRequested, [=]{document->print(printer);}); auto lay = new QVBoxLayout(this); lay->addWidget(preview); } void Window::createDocument(){ QTextCursor cursor(document); QTextTableFormat tableFormat; tableFormat.setBorder(0); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(3); QVector<QTextLength> columnLengths; columnLengths.append(QTextLength(QTextLength::PercentageLength, 40)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); tableFormat.setColumnWidthConstraints(columnLengths); int row = 10, column = 4; cursor.insertTable(row, column, tableFormat); for (int i = 0; i < column; i++) { auto cell = cursor.currentTable()->cellAt(0, i); QTextTableCellFormat cellFormat; cellFormat.setTopBorder(1); cellFormat.setBottomBorder(1); cellFormat.setBorderBrush(Qt::black); cellFormat.setTopBorderStyle(QTextFrameFormat::BorderStyle_Solid); cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid); cell.setFormat(cellFormat); QTextCharFormat textFormat; textFormat.setFontWeight(QFont::Bold); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); cursor.setBlockFormat(blockFormat); cursor.insertText("Column " + QString::number(i + 1), textFormat); cursor.movePosition(QTextCursor::NextCell); } QFontMetrics fm(font()); // w: 481 h:728 QRectF col1Rect(0,0, 481 * 0.40, fm.boundingRect("tg").height()); QPainter painter; for (int r = 1; r < row; r++) { for (int c = 0; c < column; c++) { if(c == 0){ auto col1Text = "Column1 Text Column1 Text Column1 Text Column1"; auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text); qDebug() << col1Rect.width() << col1Rect.height() << reqRect.width() << reqRect.height(); cursor.insertText(col1Text); cursor.movePosition(QTextCursor::NextCell); continue; } cursor.insertText("Row " + QString::number(r) + " Column " + QString::number(c)); cursor.movePosition(QTextCursor::NextCell); } } } Window::~Window(){}
in the second
for
loop ofcreateDocument
function, I'veqDebug() << col1Rect.width() << col1Rect.height() << reqRect.width() << reqRect.height();
and it always outputs:
192.4 16 0 0 192.4 16 0 0 ...
How to get the correct required height of the desired text?
-
@Emon-Haque said in Why does QPainter::boundingRect returns 0 0?:
QPainter painter; ----> ?????
auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text);Use instead:
QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops = 0, int *tabArray = nullptr) const -
@Emon-Haque said in Why does QPainter::boundingRect returns 0 0?:
QPainter painter; ----> ?????
auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text);Use instead:
QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops = 0, int *tabArray = nullptr) const@mpergand, yes now I get
192 16 148 32
.