Gary Kean
Gary Kean

Reputation: 51

QtGui.QFileDialog Unicode designations?

This is making me crazy and I have searched all over and can't find anyone who is having this problem. I can't really describe it well in a search to look for an answer so I am coming to you.

This is my code:

def browse(self):

    directory = QtGui.QFileDialog.getOpenFileName (self, "Find Files")

    if directory:
        self.newfilepath.setText(str(directory))
    else:
        self.newfilepath.setText('No file selected')

I am trying to put the result into a field in a window that I would then push to a sqlite db for later retrieval. Unfortunately the following code gives me a result I don't seem to be able to fix.

    name1 = (self.newdescript.toPlainText())

The result is:

(u'F:/GeoDatabase/GeoDatabase2.py', u'All Files (*.*)')

What I want is just:

F:/GeoDatabase/GeoDatabase2.py

or maybe:

'F:/GeoDatabase/GeoDatabase2.py'

How do I get it to give me just the path name and not all the other crap. Nothing I do seems to make a difference and I don't know where else to turn.

Upvotes: 1

Views: 695

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

This is caused by a little difference between PyQt4 and PySide.

With PyQt4, all of the static QFileDialog methods (such as getOpenFileName) either return a single string or (for getOpenFileNames) a list of strings.

But with PySide, getExistingDirectory returns a single string, but all the other methods return a tuple consisting of a string/list of strings, plus the selected filter.

To work around this, in PySide, your code would need to look like this:

def browse(self):
    path, filter = QtGui.QFileDialog.getOpenFileName(self, "Find Files")
    if path:
        self.newfilepath.setText(path)
    else:
        self.newfilepath.setText('No file selected')

PS:

Strictly speaking, PySide is more correct here. The C++ API modifies one of its arguments to supply the selected filter, so returning a tuple is the most pythonic way to represent that.

PyQt4 fails to honour the modified argument, but is perhaps more intuitive as a result of that.

PPS:

For PyQt5, there is no longer any difference with PySide as it now also returns a tuple of path(s) and filter.

Upvotes: 1

Related Questions