Results 1 to 5 of 5

Thread: change QGraphicsView* in QThread

  1. #1

    Default change QGraphicsView* in QThread

    I want to be able to pass a QGraphicsView* to my QThread class and be able to update the view in the thread. The QGraphicsView* obviously belongs to a gui and is not created by thread. I attempted to edit the QGraphicsView* with the thread and I get these errors in the console:

    QObject::startTimer: timers cannot be started from another thread

    It crashes 90% of the time. I read you cannot change a QWidget* in a thread it wasn't created. Fair enough. But what am I supposed to do?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: change QGraphicsView* in QThread

    No way, we can't access widgets from worker threads. You have to do all gui operations in the main thread transfering data across threads if needed using events or signals.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: change QGraphicsView* in QThread

    Ok I added a signal in my QThread class and a slot in the class that uses the QGraphicsView. First it actually adds lines to a QGraphicsScene, and I update this using a slot, and then another signal to update the QGraphicsView with a slot. My QThread emits the signals. However, it seems like more than half the signals get lost because its only drawing half the lines. It got rid of the original timer errors but it will often crash.

    The reason I'm doing this is becuase I have a QListWidget that will draw up to and over 30,000 lines to a QGraphicsScene to a QGraphicsView. This can be lengthy and if the item is changed in the list ( signal currentItemChanged ), we're wasting time waiting it for it to draw it even though its been changed! So basically I want to be able to kill the thread thats attempting to draw if the item has changed to stop wasting time and begin drawing the new one.

    Or is there a better way to do this?

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: change QGraphicsView* in QThread

    we're wasting time waiting it for it to draw it even though its been changed! So basically I want to be able to kill the thread thats attempting to draw if the item has changed to stop wasting time and begin drawing the new one.
    then see u use wait() condition in destructor of QThread ... means letting the Thread finish its pending work .. sometimes system crash when the thread killed before completed ..

    i will recommend mandlebrot example
    http://doc.trolltech.com/4.3/threads-mandelbrot.html

    will helps a lot
    "Behind every great fortune lies a crime" - Balzac

  5. #5

    Default Re: change QGraphicsView* in QThread

    Awesome, thank you wagmare. Using the mutex makes sense. I didn't know terminating a thread while its running would cause it to crash. ( I didn't actually read the api call until now, I just saw it from intellisense ). Also figured out that it wasn't drawing half the lines because I had to use Qt::BlockingQueuedConnection otherwise it just gets bombarded with signals and it gets lost. I used a function to set a boolean in the thread to tell it return from run(). Works exactly how I want. Thanks again!

Similar Threads

  1. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 17:41
  2. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 13:54
  4. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2007, 00:20
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 23: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
  •  
Qt is a trademark of The Qt Company.