Results 1 to 5 of 5

Thread: Using DrawText on a QImage in a QThread

  1. #1
    Join Date
    Apr 2007
    Location
    France
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Using DrawText on a QImage in a QThread

    Hello,

    I've been spending a week to find a solution to this issue, there's definitly no way to write some text outside of the main GUI thread. Each time I tried, I got a "Xlib: unexpected async reply" error on the call to drawText() method.

    I thought to use a signal/slot mechanism : it works when the signal is emited from the main GUI thread, but the problem is that I can't do this in my application because it's meant to be a server and its goal is to create a QImage with drawings and also some text, when a client requests it.

    Recently I've been reading this page :
    It states that there was some handlers in Qt3 to help synchronizing with the XLib, but I can't find them anymore in Qt4. Does anyone know any replacement mechanism ?

    Thanks for your feedback

    D_N_R

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using DrawText on a QImage in a QThread

    Do you call drawText from the GUI thread on the QImage from the worker thread, or from the worker thread as response to a main thread signal?

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using DrawText on a QImage in a QThread

    This is it ( from Assistant ):
    When using QPainter on a QImage, the painting can be performed in another thread than the current GUI thread, that is except rendering text (because QFont is GUI dependent). To render text in another thread, the text must first be derived as a QPainterPath in the GUI thread.
    You can use QPainterPath::addText( const QPointF&, const QFont&, const QString& ). But be careful when computing the text baseline, because your text's left end baseline will be at the QPointF passed to addText.

    Next, all you have to do is give the other thread this QPainterPath to draw it.

  4. The following user says thank you to marcel for this useful post:

    D_N_R (13th April 2007)

  5. #4
    Join Date
    Apr 2007
    Location
    France
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using DrawText on a QImage in a QThread

    You're right, QPainterPath does work when it is instanciated in the main GUI thread and another thread calls drawPath(). But as my application is a server to draw images, I'm looking for a way to modify the text/fonts to draw in function of the requests (each request is treated in its own thread).

    I thought of sending some signal to a QObject instanciated in the main thread to update a QPainterPath, but actually, the main thread of my server is a thread manager and it's waiting for some client requests. So it can't deal with any signal to modify a QPainterPath as it hasn't any QApplication::exec() running.

    I'm still stucked with this problem.

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using DrawText on a QImage in a QThread

    Call QThread::exec() in your thread manager's run method, and reimplement QObject::event in this thread. The clients should not use signals/slots to talk to the thread manager, but they should post events in it's event queue. Then, the server should create a QPainterPath for the client that posted the request.

    You can find more about this in the events documentation.

    Regards.

Similar Threads

  1. [Qt4] How to load Url image into QImage?
    By Gonzalez in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2014, 10:10
  2. Qthread Issue?
    By vishal.chauhan in forum Newbie
    Replies: 3
    Last Post: 29th March 2007, 08:50
  3. QImage constructor
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 12:06
  4. QGraphicsScene and QThread
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2007, 09:32
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.