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



  • for example:

    @
    class A : public QObject
    {
    Q_OBJECT
    // ... //
    signals:
    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);
     m_pA->moveToThread(&m_Thread);
    
     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:
    http://doc-snapshot.qt-project.org/qt5-5.4/threads-reentrancy.html#reentrant

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.