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:
Expand|Select|Wrap|Line Numbers
- 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.WaitCursor)
- 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.WaitCursor)
- _out.operator<<(self.textEdit.toPlainText())
- QtGui.QApplication.restoreOverrideCursor()
- self.setCurrentFile(fileName)
- self.statusBar().showMessage('File saved', 2000)
- return True
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