在没有GUI的情况下获取字体指标(控制台模式) [英] Getting font metrics without GUI (console mode)

查看:97
本文介绍了在没有GUI的情况下获取字体指标(控制台模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,某些图片必须由Qt控制台程序生成,并且内部算法需要字体指标(它们将文本宽度/高度用作输入计算应在其中发生绘图的位置)。此程序必须可以在没有任何GUI的Linux上运行(运行级别3,基本上是一个没有任何显示服务器的群集)。

Let's say some images have to be generated by a Qt console program and that font metrics are needed by internal algorithms (they use the text width/height as input to compute the position where the drawing should occur). This program has to be runable on a Linux without any GUI (runlevel-3, basically a cluster without any display server).

问题: QFontMetrics仅在以GUI模式运行Qt应用程序时可用。

是否有任何变通办法来获取没有任何显示服务器的字符串度量?

Problem: QFontMetrics are only available when running a Qt application in GUI mode.
Any workaround to get string metrics without any display server ?

推荐答案

好吧,在附加注释后,我想我理解您的问题。
就是这样:

Ok after additional comments I think I understand your problem. Just do it like that:

include <QApplication>

int main(int argv, char **args)
{
    QApplication app(argv, args);
    QApplication::processEvents(); // this should allow `QApplication` to complete its initialization

    // do here whatever you need 

    return 0; // or some other value to report errors
}

您也可以尝试使用 QGuiApplication 此版本不需要(不使用)小部件。

You can also try use QGuiApplication this version doesn't require (doesn't use) widgets.

另请参见文档中的示例如何不处理gui案例。

See also example in documentation how to handle none gui cases.



此代码在Qt 5.3的Ubnutu上完美运行


This code works perfectly on my Ubnutu with Qt 5.3

#include <QGuiApplication>
#include <QFontMetrics>
#include <QDebug>

int main(int argc, char *argv[])
{
    QGuiApplication a(argc, argv);

    QFont font("Times", 10, QFont::Bold);
    qDebug() << font;
    QFontMetrics metrics(font);

    qDebug() << metrics.boundingRect("test");

    return 0;
}

QApplication时,它也可与Qt 4.8配合使用

It also works with Qt 4.8 when QApplication is used.

项目文件非常简单

QT       += core
TARGET = MetricsNoGui
TEMPLATE = app
SOURCES += main.cpp

这篇关于在没有GUI的情况下获取字体指标(控制台模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆