简体   繁体   English

如何设置动态QFont大小?

[英]How to set dynamic QFont size?

I came across QFontMetrics?我遇到了 QFontMetrics? http://doc.qt.io/qt-5/qfontmetrics.html http://doc.qt.io/qt-5/qfontmetrics.html
This gives the height and width of the present font.这给出了当前字体的高度和宽度。

I need to run my application with full screen mode on on different monitors for which I am using Scale class.我需要在使用 Scale 类的不同显示器上以全屏模式运行我的应用程序。 http://doc.qt.io/qt-5/qml-qtquick-scale.html http://doc.qt.io/qt-5/qml-qtquick-scale.html

That returns the height and width of the current screen.返回当前屏幕的高度和宽度。

Is there a way to use QFontMetrics or anything else to change the font size according to the monitor size?有没有办法使用 QFontMetrics 或其他任何方法根据显示器大小更改字体大小?

ApplicationWindow
{
    id: head

    visible: true

    width:  Screen.width
    height: Screen.height

    title: "Novus Pilot"

    property var id: 0;

    Draw_on_qimage
    {
        id: draw_on_qimage
        anchors.fill: parent
        parent: image

        scaleX: head.width / 640
        scaleY: head.height / 480
    }
}

Draw_on_qimage is a cpp class. Draw_on_qimage是一个 cpp 类。

The easiest way is to set QFont as Q_PROPERTY of your item so you can set it from QML:最简单的方法是将 QFont 设置为您项目的 Q_PROPERTY,以便您可以从 QML 设置它:

#ifndef DRAWITEM_H
#define DRAWITEM_H

#include <QPainter>
#include <QQuickPaintedItem>

class DrawItem : public QQuickPaintedItem
{
    Q_OBJECT
    Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
public:
    DrawItem(QQuickItem *parent = Q_NULLPTR):QQuickPaintedItem(parent){}
    void paint(QPainter *painter){
        painter->setFont(mFont);
        painter->drawText(boundingRect(), "Hello");
    }
    QFont font() const{
        return mFont;
    }
    void setFont(const QFont &font){
        if(mFont == font)
            return;
        mFont = font;
        emit fontChanged();
        update();
    }

signals:
    void fontChanged();
private:
    QFont mFont;
};

#endif // DRAWITEM_H

To set its size we use the pointSize property of QFont:要设置它的大小,我们使用 QFont 的 pointSize 属性:

DrawItem
{
    id: draw_on_qimage
    anchors.fill: parent
    font.pointSize: some_function(head.width, head.height)
    transform: Scale {
        xScale: head.width / 640
        yScale: head.height / 480
    }
}

Where some_function is the function that establishes the relationship between the font size and the size of the window.其中 some_function 是建立字体大小和窗口大小之间关系的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM