Qt World Summit: Register Today!

Is it safe to use QFont / QFontMetrics in a NON-GUI thread ?

  • for example:

    class A : public QObject
    // ... //
    void oneEntryFound(QString, int);

    public slots:
    void loadContent();

    void A::loadContent()
    QFont f("Segoe UI", 9);
    QFontMetrics fm(f);

    QuaZip zipFile(m_sFilePath);
    bool bOK = zipFile.goToFirstFile();
    while (bOK) {
         QString fn = zipFile.getCurrentFileName();
         emit oneEntryFound(fn, fm.width(fn));
         bOK = zipFile.goToNextFile();


    and somewhere

    void MyWidget::openZip(const QString& fn)
    if (m_pA) m_pA->deleteLater();

     m_pA = new A(fn);
     connect(this, &MyWidget::blah, m_pA, &A::loadContent);
     connect(m_pA, &A::oneEntryFound, 
            [this] (QString fn, int w) { m_cW = qMax(m_cW, w); } 
     emit blah();


    Thanks in advance.

  • Looking into the Qt docs will explain that QFont and QFontMetrics are reentrant:

    I think as long as you don't modify your QFont/QFontMetrics object from another threads this is okay.

Log in to reply