PyQt4 trouble with save and load file dialogs...
Question posted by: blazedaces
(Needs Regular Fix)
on
August 27th, 2008 09:10 PM
I'm designing a GUI for autonomous robot interaction, but that's not so important. To figure out the ins and outs of a certain aspect of PyQt I usually just do what I did when I learning Qt, I grab an example from the site and literally copy the code.
Unfortunately, PyQt isn't written exactly word-for-word as Qt is written in C++, obviously. So there's a slight conversation that I do to make it work. This can lead to errors at times. I've done it many times before, but now I'm having trouble.
So anyway, to get to the point, I'm programming this Application Example and the GUI actually starts up fine. Text can be typed, cut, copied and pasted with "Ctr+C", "Ctr+X", "Ctrl+V", even though the actual buttons for those do not work (in the toolbars and in the menu bars) that's not the problem I want to tackle at the moment.
When I try to open a file, it pops up with an error message, even though it should be possible to open a normal .txt file. If I try to save a file, it pops up with the same error message (which is reported as unknown).
Here's the code for the loadFile and saveFile methods where there's an if statement where if the file does not meet certain requirements an error message should arise:
- def loadFile(self, fileName):
-
file = QtCore.QFile(fileName)
-
if ~file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text):
-
QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot read file %1:\n%2.').arg(fileName).arg(file.errorString()))
-
return
-
-
_in = QtCore.QTextStream(file)
-
QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai tCursor)
-
self.textEdit.setPlainText(_in.readAll())
-
QtGui.QApplication.restoreOverrideCursor()
-
-
self.setCurrentFile(fileName)
-
self.statusBar().showMessage('File loaded', 2000)
-
-
def saveFile(self, fileName):
-
file = QtCore.QFile(fileName)
-
if ~file.open(QtCore.QIODevice.WriteOnly | QtCore.QIODevice.Text):
-
QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot write file %1:\n%2.').arg(fileName).arg(file.errorString()))
-
return False
-
-
_out = QtCore.QTextStream(file)
-
QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai tCursor)
-
_out.operator<<(self.textEdit.toPlainText())
-
QtGui.QApplication.restoreOverrideCursor()
-
-
self.setCurrentFile(fileName)
-
self.statusBar().showMessage('File saved', 2000)
-
return True
If anyone can help me identify why, even though everything looks fine, it still pops up with an error every time, I would be very grateful.
Also, for more information, after I save, if I look in the directory where it was supposed to be saved a file exists there, but when trying to open the file, it shows up blank, as in it never contains the text that was supposed to be saved.
I've attempted to debug this slightly and I know it never leaves the inside of the if statement (it exits the method at "return" or "return False") as it makes sense it should if an error message comes up.
Thanks for your help,
-blazed
2
Answers Posted
If no one can help me with this can someone please tell me what would be a good source for this problem?
Thanks a bunch,
-blazed
Alright, I found a solution to the problem:
While it's nice to be able to use PyQt's I/O capabilities python has its own, so I used those instead and it worked just as needed:
- def loadFile(self, fileName):
-
try:
-
file = open(fileName, 'r')
-
except IOError, (filename, message):
-
QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot read file %1:\n%2.').arg(filename).arg(message))
-
except:
-
print "Unexpected error:", sys.exc_info()[0]
-
raise
-
else:
-
QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai tCursor)
-
self.textEdit.setPlainText(string.join(file.readli nes()))
-
QtGui.QApplication.restoreOverrideCursor()
-
-
self.setCurrentFile(fileName)
-
self.statusBar().showMessage('File loaded', 2000)
-
finally:
-
file.close()
-
-
def saveFile(self, fileName):
-
try:
-
file = open(fileName, 'w')
-
except IOError, (filename, message):
-
QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot write file %1:\n%2.').arg(filename).arg(message))
-
except:
-
print "Unexpected error:", sys.exc_info()[0]
-
raise
-
else:
-
QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai tCursor)
-
file.write(self.textEdit.toPlainText())
-
QtGui.QApplication.restoreOverrideCursor()
-
-
self.setCurrentFile(fileName)
-
self.statusBar().showMessage('File saved', 2000)
-
return True
-
finally:
-
file.close()
Works great.
Anyway, now my issue is the one I mentioned earlier with the cut/copy/paste mechanisms of Qt's textEditor.
I've noticed something odd that maybe will give someone a clue: of the three commands, only "cut" partially works. You see, paste does not paste from the clipboard and copy does not copy the selected text onto the clipboards. On the other hand, cut, for whatever reason, does indeed put the selected text onto the clipboard, but it does not remove the text selected (it doesn't cut, it copies), though the cursor itself moves, as in it no longer selects the text it previously did.
This might sound a bit confusing. I hope someone might be able to help, thanks guys.
-blazed
 |
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 197,034 network members.
|