QPrinter&QPainter. Size scaling
Unsolved
General and Desktop
-
I want render information from QTableWidget to pdf file using QPrinter + QPainter, but i have a problem with size scaling when trying do that, they little more than in qtablewidget (~10%). How i can solve this problem? I guess that problem in scale coefficients for QPainter. Thanks for answers.
TableWidget
TableWidget and Pdf comparison
Source code for converting:
QPrinter printer(QPrinter::HighResolution); printer.setOutputFileName(""); printer.setPageSize(QPrinter::A4); printer.setFullPage(true); QPainter painter(&printer); const auto logicalDeviceDpiX = printer.logicalDpiX(); const auto logicalDeviceDpiY = printer.logicalDpiY(); auto screen = QApplication::screens().at(0); const auto logicalScreenDpiX = screen->logicalDotsPerInchX(); const auto logicalScreenDpiY = screen->logicalDotsPerInchY(); const auto scaleWidth = logicalDeviceDpiX / logicalScreenDpiX; const auto scaleHeight = logicalDeviceDpiY / logicalScreenDpiY; painter.scale(scaleWidth, scaleHeight); qDebug() << "Logical: "; qDebug() << "Printer dpix: " << printer.logicalDpiX(); qDebug() << "Printer dpiy: " << printer.logicalDpiY(); qDebug() << "Physical"; qDebug() << "Printer dpix: " << printer.physicalDpiX(); qDebug() << "Printer dpiy: " << printer.physicalDpiY(); qDebug() << "Logical: "; qDebug() << "Table widget dpix: " << tableWidget->logicalDpiX(); qDebug() << "Table widget dpiy: " << tableWidget->logicalDpiY(); qDebug() << "Physical"; qDebug() << "Table widget dpix: " << tableWidget->physicalDpiX(); qDebug() << "Table widget dpiy: " << tableWidget->physicalDpiY(); auto rowOffset = 0.0f; auto columnOffset = 0.0f; QPen pen(Qt::black); pen.setWidth(0); painter.setPen(pen); for (auto rowIndex = 0; rowIndex < rowCount; ++rowIndex) { auto rowHeight = tableWidget->rowHeight(rowIndex); columnOffset = 0.0f; for (auto columnIndex = 0; columnIndex < columnCount; ++columnIndex) { auto columnWidth = tableWidget->columnWidth(columnIndex); QRectF cellRect(columnOffset, rowOffset, columnWidth, rowHeight); painter.drawRect(cellRect); QFont font("Times", 12); QFontMetrics metrics(font); font.setPixelSize(metrics.height()); painter.setFont(font); painter.drawText(cellRect, tableWidget->item(rowIndex, columnIndex)->text(), Qt::AlignHCenter | Qt::AlignVCenter); columnOffset += columnWidth; } rowOffset += rowHeight; }