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:
[code=python] def loadFile(self, fileName):
file = QtCore.QFile(fi leName)
if ~file.open(QtCo re.QIODevice.Re adOnly | QtCore.QIODevic e.Text):
QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot read file %1:\n%2.').arg( fileName).arg(f ile.errorString ()))
return
_in = QtCore.QTextStr eam(file)
QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
self.textEdit.s etPlainText(_in .readAll())
QtGui.QApplicat ion.restoreOver rideCursor()
self.setCurrent File(fileName)
self.statusBar( ).showMessage(' File loaded', 2000)
def saveFile(self, fileName):
file = QtCore.QFile(fi leName)
if ~file.open(QtCo re.QIODevice.Wr iteOnly | QtCore.QIODevic e.Text):
QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot write file %1:\n%2.').arg( fileName).arg(f ile.errorString ()))
return False
_out = QtCore.QTextStr eam(file)
QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
_out.operator<< (self.textEdit. toPlainText())
QtGui.QApplicat ion.restoreOver rideCursor()
self.setCurrent File(fileName)
self.statusBar( ).showMessage(' File saved', 2000)
return True[/code]
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
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:
[code=python] def loadFile(self, fileName):
file = QtCore.QFile(fi leName)
if ~file.open(QtCo re.QIODevice.Re adOnly | QtCore.QIODevic e.Text):
QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot read file %1:\n%2.').arg( fileName).arg(f ile.errorString ()))
return
_in = QtCore.QTextStr eam(file)
QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
self.textEdit.s etPlainText(_in .readAll())
QtGui.QApplicat ion.restoreOver rideCursor()
self.setCurrent File(fileName)
self.statusBar( ).showMessage(' File loaded', 2000)
def saveFile(self, fileName):
file = QtCore.QFile(fi leName)
if ~file.open(QtCo re.QIODevice.Wr iteOnly | QtCore.QIODevic e.Text):
QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot write file %1:\n%2.').arg( fileName).arg(f ile.errorString ()))
return False
_out = QtCore.QTextStr eam(file)
QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
_out.operator<< (self.textEdit. toPlainText())
QtGui.QApplicat ion.restoreOver rideCursor()
self.setCurrent File(fileName)
self.statusBar( ).showMessage(' File saved', 2000)
return True[/code]
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
Comment