python QApplicationPrivate::notify
I have built a python app with PyQt4 library.
The app has a graphics scene with a picture on it. I then draw lines over the scene and I catch mouse movements to get the coordinates of the mouse location over the picture.
I attach the mousehover like this:
pmItem = QGraphicsPixmapItem(pm, None, self.scene)
pmItem.setAcceptHoverEvents(True)
pmItem.hoverMoveEvent = self.pixelSelect
pmItem.hoverLeaveEvent = self.hover_leave
The pixelSelect
method takes the current coordinates and measure the distance to the closest vertical and horizontal lines and draw lines from the current point to theses lines.
And this is the pixelSelect
method:
def pixelSelect(self, event):
for l in self.v_dist_lines:
self.scene.removeItem(l)
self.v_dist_lines = []
for l in self.h_dist_lines:
self.scene.removeItem(l)
self.h_dist_lines = []
for t in self.txt_dist_list:
self.scene.removeItem(t)
self.txt_dist_list = []
position = QtCore.QPoint(event.pos().x(), event.pos().y())
closest_h_line, closest_v_line = None, None
dist_h, dist_v = self.scene_h, self.scene_w
for l in self.h_lines_list:
diff = math.fabs(position.y() - l.line().y1())
if diff < dist_h:
dist_h = diff
closest_h_line = l
for l in self.v_lines_list:
diff = math.fabs(position.x() - l.line().x1())
if diff < dist_v:
dist_v = diff
closest_v_line = l
diff_h = round(float(dist_h) / QX11Info.appDpiY() * self.in2cm * (self.draw_scale_y / self.hcm), 2)
diff_v = round(float(dist_v) / QX11Info.appDpiX() * self.in2cm * (self.draw_scale_x / self.wcm), 2)
# print(diff_h, diff_v)
result = ''
if closest_h_line:
result += 'Distance to nearest horizontal line is {:0.2f} cm'.format(diff_h)
result += ',t'
self.drawLine(position.x(), position.y(),
position.x(), closest_h_line.line().y1(), self.h_dist_lines)
txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_h)),
scene=self.scene)
txt_x = position.x() + 10
y_diff = abs(position.y() - closest_h_line.line().y1()) / 2
txt_y = position.y() + y_diff if closest_h_line.line().y1() > position.y()
else position.y() - y_diff
txt.setPos(QtCore.QPointF(txt_x,
txt_y))
self.txt_dist_list.append(txt)
if closest_v_line:
result += 'Distance to nearest vertical line is {:0.2f} cm'.format(diff_v)
self.drawLine(position.x(), position.y(),
closest_v_line.line().x1(), position.y(), self.v_dist_lines)
txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_v)),
scene=self.scene)
x_diff = abs(position.x() - closest_v_line.line().x1()) / 2
txt_x = position.x() + x_diff if closest_h_line.line().x1() > position.x()
else position.x() - x_diff
txt_y = position.y() + 10
txt.setPos(QtCore.QPointF(txt_x,
txt_y))
self.txt_dist_list.append(txt)
self.statusBar().showMessage(result)
When I run the app, it runs perfect for a few seconds (sometimes with the very first mouse movement) then it crashes while I'm moving the cursor inside the scene. I noticed it doesn't crash when there are no lines drawn over the scene. The method pixelSelect
is still running even if there are no lines so I don't think that the problem is in the method.
When I debugged using gdb
, I got this when I ran the porgram:
Starting program: /usr/bin/python main.py [Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe4048700 (LWP 9767)]
[New Thread 0x7fffe3847700 (LWP 9768)]
[New Thread 0x7fffe3046700 (LWP 9769)]
[New Thread 0x7fffd3fff700 (LWP 9770)]
[New Thread 0x7fffd37fe700 (LWP 9771)]
[Thread 0x7fffd3fff700 (LWP 9770) exited]
[New Thread 0x7fffd3fff700 (LWP 9792)]
[New Thread 0x7fffcbfff700 (LWP 9793)]
[New Thread 0x7fffcb7fe700 (LWP 9794)]
[New Thread 0x7fffcaffd700 (LWP 9795)]
[Thread 0x7fffd3fff700 (LWP 9792) exited]
[Thread 0x7fffcb7fe700 (LWP 9794) exited]
[Thread 0x7fffcbfff700 (LWP 9793) exited]
[Thread 0x7fffcaffd700 (LWP 9795) exited]
[Thread 0x7fffd37fe700 (LWP 9771) exited]
and when it crashes this is the output of the bt
:
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
0 0x0000000000000000 in ?? ()
1 0x00007ffff3d28fdc in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
2 0x00007ffff3d2ff16 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
3 0x00007ffff4e15f56 in ?? () from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
4 0x00007ffff636790d in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtCore.so.4
5 0x00007ffff434884d in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
6 0x00007ffff4350b91 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
7 0x00007ffff4350f1f in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
8 0x00007ffff435146a in QGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
9 0x00007ffff4cd839b in ?? () from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
10 0x00007ffff435e607 in QGraphicsScene::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
11 0x00007ffff4cd9c6b in ?? () from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
12 0x00007ffff3d28fdc in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
13 0x00007ffff3d2ff16 in QApplication::notify(QObject*, QEvent*) ()
---Type return to continue, or q return to quit---
So, it is something with QApplicationPrivate::notify_helper
method. Can any one help with this problem? I know it is a SEGSEGV
error which means memory issues. But the same code runs the same way all the time on the same data. But it crashes in different times with different cases which makes it difficult to know what is abusing the memory.
UPDATE
I added the code to a github repo:
https://github.com/Ahmedn1/Image_Splitter