PDA

View Full Version : segmentation fault on mouse hover



elessaar
25th August 2008, 12:49
im gonna explain my structure step by step

i have a widget including a button and a label
label has a png as background.
when i click on button a thread is being created
that thread draws a line chart on that label

here is my problem:
when i move my mouse on the png ,my application exits with segmentation fault


i read data from an array to draw the chart. i update array in another thread.
i have 2 mutexes to make my threads work consecutively. "1 update 1 draw" i mean.
draw function adds lines to my scene/view

another problem:
if i dont call msleep(30+) after each loop in each thread my program exits with fault again
30+ = ms under 30 doesnt work also :(

if needed i can post my code.
i need ur help.

have a good day every1

ersin özkan
turkey

wysota
25th August 2008, 13:54
Worker threads are forbidden from drawing anything on widgets. You have to draw from within the main thread.

elessaar
25th August 2008, 14:01
but main thread doesnt update the scene immediately after my thread finish adding lineitems. so i call update() inside thread and it works. but when i mouse over i get error

wysota
25th August 2008, 14:38
There is no "but". You simply can't draw from within worker threads and that's final or else you'll be getting crashes at random moments and that's exactly what is happening. Use signals and slots or custom events to transfer data across threads.

elessaar
26th August 2008, 12:41
wysota; should i do the "addLine to scene" work in main widget also?

ty for help

aamer4yu
26th August 2008, 13:46
I am not sure if this will work -
Pass a canvas / paint device to the thread, and use it in the paint event of the main widget.
The thread will draw on the canvas while the canvas is member of main widget, hence it can be accessed from the paint event of main widget. :)

wysota
26th August 2008, 13:51
wysota; should i do the "addLine to scene" work in main widget also?

All QWidgets are not thread-safe (nor even reentrant as far as I remember) thus all actions related to widgets have to be done from the main thread unless the docs explicitely state otherwise for a particular operation. QGraphicsScene is not a QWidget but it is tightly related to it, so it might not be safe to interface it directly from an external thread. I really suggest you stick to signals and slots if you need multithreading (if you do).