python QApplicationPrivate :: notify
我已经用PyQt4库构建了一个python应用程序。
该应用程序有一个图形场景和图片。 然后我在场景上画线,然后捕捉鼠标移动以获得鼠标在画面上的位置坐标。
我附上这样的mousehover:
pmItem = QGraphicsPixmapItem(pm, None, self.scene)
pmItem.setAcceptHoverEvents(True)
pmItem.hoverMoveEvent = self.pixelSelect
pmItem.hoverLeaveEvent = self.hover_leave
pixelSelect
方法获取当前坐标并测量到最近的垂直和水平线的距离,并从当前点到这些线绘制线。
这是pixelSelect
方法:
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)
当我运行该应用程序时,它会运行几秒钟(有时只是第一次鼠标移动),然后在将光标移动到场景内时崩溃。 我注意到,当场景中没有画线时,它不会崩溃。 即使没有行, pixelSelect
方法仍在运行,所以我不认为问题出在该方法中。
当我使用gdb
调试时,当我运行porgram时,我得到了这个:
启动程序:/ usr / bin / python main.py [使用libthread_db启用线程调试]
使用主机libthread_db库“/lib/x86_64-linux-gnu/libthread_db.so.1”。
[新线程0x7fffe4048700(LWP 9767)]
[新线程0x7fffe3847700(LWP 9768)]
[新线程0x7fffe3046700(LWP 9769)]
[新线程0x7fffd3fff700(LWP 9770)]
[新线程0x7fffd37fe700(LWP 9771)]
[线程0x7fffd3fff700(LWP 9770)退出]
[新线程0x7fffd3fff700(LWP 9792)]
[新线程0x7fffcbfff700(LWP 9793)]
[新线程0x7fffcb7fe700(LWP 9794)]
[新线程0x7fffcaffd700(LWP 9795)]
[线程0x7fffd3fff700(LWP 9792)退出]
[线程0x7fffcb7fe700(LWP 9794)已退出]
[线程0x7fffcbfff700(LWP 9793)已退出]
[线程0x7fffcaffd700(LWP 9795)退出]
[线程0x7fffd37fe700(LWP 9771)退出]
当它崩溃时,这是bt
的输出:
线程1“python”收到信号SIGSEGV,分段错误。
0x0000000000000000在?? ()
(gdb)bt
0 0x0000000000000000在? ()
来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4的QApplicationPrivate :: notify_helper(QObject *,QEvent *)()中的1 0x00007ffff3d28fdc
来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4的QApplication :: notify(QObject *,QEvent *)()中的2 0x00007ffff3d2ff16
3 0x00007ffff4e15f56在? ()from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
来自/usr/lib/x86_64-linux-gnu/libQtCore.so.4的QCoreApplication :: notifyInternal(QObject *,QEvent *)()中的4 0x00007ffff636790d
5 0x00007ffff434884d在?? ()来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4
6 0x00007ffff4350b91在?? ()来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4
7 0x00007ffff4350f1f在?? ()来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4
8在QGraphicsScene中的0x00007ffff435146a :: mouseMoveEvent(QGraphicsSceneMouseEvent *)()from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
9 0x00007ffff4cd839b在?? ()from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4的QGraphicsScene :: event(QEvent *)()中的10 0x00007ffff435e607
11 0x00007ffff4cd9c6b在?? ()from /usr/lib/python2.7/dist-packages/PyQt4/QtGui.so
来自/usr/lib/x86_64-linux-gnu/libQtGui.so.4的QApplicationPrivate :: notify_helper(QObject *,QEvent *)()中的12 0x00007ffff3d28fdc
QApplication :: notify(QObject *,QEvent *)中的13 0x00007ffff3d2ff16()
---类型返回继续,或者q返回退出---
所以,这与QApplicationPrivate::notify_helper
方法有关。 任何人都可以帮助解决这个问题吗? 我知道这是一个SEGSEGV
错误,这意味着内存问题。 但是相同的代码始终以相同的方式在相同的数据上运行。 但它在不同的情况下会在不同的情况下崩溃,这使得很难知道什么是滥用内存。
UPDATE
我将代码添加到github repo中:
https://github.com/Ahmedn1/Image_Splitter