Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it safe to use QFont / QFontMetrics in a NON-GUI thread ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 871 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    freeDNA
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      euchkatzl
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved