WYSIWYG with Qt - font size issues

I create my own Qt widget that mimics an A4 printed page, and I'm having trouble getting the right size fonts. My widget uses QPainter::setViewportand QPainter::setWindowto mimic an A4 page using units of 10 millimeters that allow me to easily draw. However, trying to create a font with a specific dot size does not work, and the use is QFont:setPixelSizeinaccurate. Here is the code:

View::View(QWidget *parent) :
    QWidget(parent),
    printer(new QPrinter)
{
    printer->setPaperSize(QPrinter::A4);
    printer->setFullPage(true);
}

void View::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setWindow(0, 0, 2100, 2970);
    painter.setViewport(0, 0, printer->width(), printer->height());
    // Draw a rect at x = 1cm, y = 1cm, 6cm wide and 1 inch high
    painter.drawRect(100, 100, 600, 254);

    // Create a 72pt (1 inch) high font
    QFont font("Arial");
    font.setPixelSize(254);
    painter.setFont(font);
    // Draw in the same box
    // The font is too large
    painter.drawText(QRect(100, 100, 600, 254), tr("Wg\u0102"));
    // Ack - the actual font size reported by the metrics is 283 pixels!
    const QFontMetrics fontMetrics = painter.fontMetrics();
    qDebug() << "Font height = " << fontMetrics.height();
}

So, I ask you to get 254 tall font (1 inch, 72 points), and it is too big and sure enough, when I request the font height through QFontMetrics, it is 283 high.

- , , ? . , , / (, Win32 DPtoLP/LPtoDP.)

: , , . , , , . , , .

+3
1

QFont , . QFontMetrics:: height() 284 , QFontInfo:: pixelSize() 254, . , , height() , pixelSize() , , .

, , , , , . , . WYSIWYG, .

this. , dpi logicalDpiX() logicalDpiY() ( ).

+1

Source: https://habr.com/ru/post/1747615/


All Articles