PyQt4 trouble with save and load file dialogs...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazedaces
    Contributor
    • May 2007
    • 284

    PyQt4 trouble with save and load file dialogs...

    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
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    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

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      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:

      [code=python]def loadFile(self, fileName):
      try:
      file = open(fileName, 'r')
      except IOError, (filename, message):
      QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot read file %1:\n%2.').arg( filename).arg(m essage))
      except:
      print "Unexpected error:", sys.exc_info()[0]
      raise
      else:
      QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
      self.textEdit.s etPlainText(str ing.join(file.r eadlines()))
      QtGui.QApplicat ion.restoreOver rideCursor()

      self.setCurrent File(fileName)
      self.statusBar( ).showMessage(' File loaded', 2000)
      finally:
      file.close()

      def saveFile(self, fileName):
      try:
      file = open(fileName, 'w')
      except IOError, (filename, message):
      QtGui.QMessageB ox.warning(self , 'Application', QtCore.QString( 'Cannot write file %1:\n%2.').arg( filename).arg(m essage))
      except:
      print "Unexpected error:", sys.exc_info()[0]
      raise
      else:
      QtGui.QApplicat ion.setOverride Cursor(QtCore.Q t.WaitCursor)
      file.write(self .textEdit.toPla inText())
      QtGui.QApplicat ion.restoreOver rideCursor()

      self.setCurrent File(fileName)
      self.statusBar( ).showMessage(' File saved', 2000)
      return True
      finally:
      file.close()[/code]

      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

      Comment

      Working...