Segmentation fault in Pyqt

I am getting a segmentation fault when I try to declare a new image name. I can replace a existing image correctly. Why am I getting the error at the getSaveFileName dialog ??

Can anyone help me with the code.

import sys

from PyQt4 import QtGui, QtCore, Qt

import numpy as np

from matplotlib.figure import Figure

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar

def randrange(n, vmin, vmax):

return (vmax-vmin)*np.random.rand(n) + vmin

class Example(QtGui.QWidget):

def __init__(self):

	super(Example, self).__init__()

	self.initUI()

    

def initUI(self):

	self.main_frame = QtGui.QWidget()

	self.dpi=100

	self.i=1

	self.fig = Figure((5.0, 5.0), dpi=self.dpi)

	self.scatterpts = []

	self.canvas = FigureCanvas(self.fig)

	self.canvas.setParent(self.main_frame)

	[self.ax](http://self.ax) = self.fig.gca(projection='3d')

	self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

	self.btn = QtGui.QPushButton('Button', self)

	self.btn.resize(self.btn.sizeHint())

	self.btn.clicked.connect(self.drawplot)

	self.btn1 = QtGui.QPushButton('Save', self)

	self.btn1.resize(self.btn.sizeHint())

	self.btn1.clicked.connect(self.saveplot)

    

	self.setWindowTitle('Tooltips')    

	Box =QtGui.QVBoxLayout()

	Box.addWidget(self.canvas)

	Box.addWidget(self.btn)

	Box.addWidget(self.btn1)

	Box.addWidget(self.mpl_toolbar)

	self.main_frame.setLayout(Box)

	self.main_frame.show()

def drawplot(self):

	if self.i==0:

		for pt in self.scatterpts :

			pt.remove()

	self.scatterpts = []

	n = 500

	for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:

		xs = randrange(n, 23, 32)

		ys = randrange(n, 0, 100)

		zs = randrange(n, zl, zh)

	pt = self.ax.scatter(xs, ys, zs, c=c, marker=m, s=40)

	self.scatterpts.append(pt)

	self.ax.set_xlabel('X Label')

	self.ax.set_ylabel('Y Label')

	self.ax.set_zlabel('Z Label')

	self.canvas.draw()

	self.canvas.print_figure('trial.png', dpi=self.dpi)

	self.i=0

def saveplot(self):

	fname = QtGui.QFileDialog.getSaveFileName(self, 'Save file', '/home/untitled.png', 'Images (*.png *.xpm *.jpg)')

	self.canvas.print_figure(fname, dpi=100)

def main():

app = QtGui.QApplication(sys.argv)

ex = Example()

sys.exit(app.exec_())

if name == ‘main’:

main()

I am getting a segmentation fault when I try to declare a new image name. I
can replace a existing image correctly. Why am I getting the error at the
getSaveFileName dialog ??

[...]

fname = QtGui.QFileDialog.getSaveFileName(self, 'Save file',
'/home/untitled.png', 'Images (*.png *.xpm *.jpg)')

Here is the call signature:

QString getSaveFileName (QWidget parent = None, QString caption =
QString(), QString directory = QString(), QString filter = QString(),
Options options = 0)

You are passing a file name to a function argument that is expecting a
directory. Maybe that has something to do with it. Although, I don't
know why that would cause a crash, unless there is a bug in Qt/PyQt.

Darren

···

On Mon, Mar 12, 2012 at 4:30 PM, Sourabh Bajaj <sb2nov@...287...> wrote:

I think that QtGui.QFileDialog.getSaveFileName returns a tuple, the first element of which is the file name. You can ignore the second element by using:

fname, _ = QtGui.QFileDialog.getSaveFileName(self, 'Save file', '/home/untitled.png', 'Images (*.png *.xpm *.jpg)')

Ray

···

On Mar 12, 2012, at 4:24 PM, Darren Dale wrote:

On Mon, Mar 12, 2012 at 4:30 PM, Sourabh Bajaj <sb2nov@...287...> wrote:

I am getting a segmentation fault when I try to declare a new image name. I
can replace a existing image correctly. Why am I getting the error at the
getSaveFileName dialog ??

[...]

fname = QtGui.QFileDialog.getSaveFileName(self, 'Save file',
'/home/untitled.png', 'Images (*.png *.xpm *.jpg)')

Here is the call signature:

QString getSaveFileName (QWidget parent = None, QString caption =
QString(), QString directory = QString(), QString filter = QString(),
Options options = 0)

You are passing a file name to a function argument that is expecting a
directory. Maybe that has something to do with it. Although, I don't
know why that would cause a crash, unless there is a bug in Qt/PyQt.

Darren

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Ray Osborn
Materials Science Division
Argonne National Laboratory
Argonne, IL 60439, USA
Phone: +1 (630) 252-9011
Email: ROsborn@...3972...

It is a problem with the dialog, if you set the option DontUseNativeDialog. then it works fine.

I guess that the problem is with Ubuntu 11.10.

···

On Mon, Mar 12, 2012 at 5:37 PM, Ray Osborn <ROsborn@…3972…> wrote:

I think that QtGui.QFileDialog.getSaveFileName returns a tuple, the first element of which is the file name. You can ignore the second element by using:

fname, _ = QtGui.QFileDialog.getSaveFileName(self, ‘Save file’, ‘/home/untitled.png’, ‘Images (*.png *.xpm *.jpg)’)

Ray

On Mar 12, 2012, at 4:24 PM, Darren Dale wrote:

On Mon, Mar 12, 2012 at 4:30 PM, Sourabh Bajaj <sb2nov@…287…> wrote:

I am getting a segmentation fault when I try to declare a new image name. I

can replace a existing image correctly. Why am I getting the error at the

getSaveFileName dialog ??

[…]

fname = QtGui.QFileDialog.getSaveFileName(self, ‘Save file’,

‘/home/untitled.png’, ‘Images (*.png *.xpm *.jpg)’)

Here is the call signature:

QString getSaveFileName (QWidget parent = None, QString caption =

QString(), QString directory = QString(), QString filter = QString(),

Options options = 0)

You are passing a file name to a function argument that is expecting a

directory. Maybe that has something to do with it. Although, I don’t

know why that would cause a crash, unless there is a bug in Qt/PyQt.

Darren


Try before you buy = See our experts in action!

The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,

Metro Style Apps, more. Free future releases when you subscribe now!

http://p.sf.net/sfu/learndevnow-dev2


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Ray Osborn

Materials Science Division

Argonne National Laboratory

Argonne, IL 60439, USA

Phone: +1 (630) 252-9011

Email: ROsborn@…3972…