好的,经过补充评论后,我想我理解你的问题。
就这样做:
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
此版本不需要(不使用)小部件。
也可以看看文档中的示例 http://doc.qt.io/qt-5/qapplication.html#details如何处理没有 GUI 的情况。
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;
}
它也适用于 Qt 4.8QApplication
用来。
项目文件非常简单
QT += core
TARGET = MetricsNoGui
TEMPLATE = app
SOURCES += main.cpp