sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
blazedaces's Avatar

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:

Expand|Select|Wrap|Line Numbers
  1.     def loadFile(self, fileName):
  2.         file = QtCore.QFile(fileName)
  3.         if ~file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text):
  4.             QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot read file %1:\n%2.').arg(fileName).arg(file.errorString()))
  5.             return
  6.  
  7.         _in = QtCore.QTextStream(file)
  8.         QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai  tCursor)
  9.         self.textEdit.setPlainText(_in.readAll())
  10.         QtGui.QApplication.restoreOverrideCursor()
  11.  
  12.         self.setCurrentFile(fileName)
  13.         self.statusBar().showMessage('File loaded', 2000)
  14.  
  15.     def saveFile(self, fileName):
  16.         file = QtCore.QFile(fileName)
  17.         if ~file.open(QtCore.QIODevice.WriteOnly | QtCore.QIODevice.Text):
  18.             QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot write file %1:\n%2.').arg(fileName).arg(file.errorString()))
  19.             return False
  20.  
  21.         _out = QtCore.QTextStream(file)
  22.         QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai  tCursor)
  23.         _out.operator<<(self.textEdit.toPlainText())
  24.         QtGui.QApplication.restoreOverrideCursor()
  25.  
  26.         self.setCurrentFile(fileName)
  27.         self.statusBar().showMessage('File saved', 2000)
  28.         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
blazedaces's Avatar
blazedaces August 28th, 2008 02:24 PM
Needs Regular Fix - 275 Posts
#2: Re: PyQt4 trouble with save and load file dialogs...

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
blazedaces's Avatar
blazedaces August 29th, 2008 08:08 PM
Needs Regular Fix - 275 Posts
#3: Re: PyQt4 trouble with save and load file dialogs...

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:

Expand|Select|Wrap|Line Numbers
  1. def loadFile(self, fileName):
  2.         try:
  3.             file = open(fileName, 'r')
  4.         except IOError, (filename, message):
  5.             QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot read file %1:\n%2.').arg(filename).arg(message))
  6.         except:
  7.             print "Unexpected error:", sys.exc_info()[0]
  8.             raise
  9.         else:
  10.             QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai  tCursor)
  11.             self.textEdit.setPlainText(string.join(file.readli  nes()))
  12.             QtGui.QApplication.restoreOverrideCursor()
  13.  
  14.             self.setCurrentFile(fileName)
  15.             self.statusBar().showMessage('File loaded', 2000)
  16.         finally:
  17.             file.close()
  18.  
  19.     def saveFile(self, fileName):
  20.         try:
  21.             file = open(fileName, 'w')
  22.         except IOError, (filename, message):
  23.             QtGui.QMessageBox.warning(self, 'Application', QtCore.QString('Cannot write file %1:\n%2.').arg(filename).arg(message))
  24.         except:
  25.             print "Unexpected error:", sys.exc_info()[0]
  26.             raise
  27.         else:
  28.             QtGui.QApplication.setOverrideCursor(QtCore.Qt.Wai  tCursor)
  29.             file.write(self.textEdit.toPlainText())
  30.             QtGui.QApplication.restoreOverrideCursor()
  31.  
  32.             self.setCurrentFile(fileName)
  33.             self.statusBar().showMessage('File saved', 2000)
  34.             return True
  35.         finally:
  36.             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
Reply
Not the answer you were looking for? Post your question . . .
197,034 members ready to help you find a solution.
Join Bytes.com

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.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Python Contributors