Wednesday, May 21, 2014

Not Dead, the Blog

Apologies for the gap between posts. A couple of good coding sessions and I nearly have the Book and Main Window modules under control. A bit of tidying and I will have something to show in a day or so.

In the meantime, while working with the tedious and complex business of opening a file, I updated my improved QTextStream class. This started as a simple work-around of the annoying problem that a QTextStream, when based on a QFile, does not take ownership of the QFile. If the Python programmer carelessly lets the QFile go out of scope, the next attempt to use the QTextStream: what? produces a log message? returns an error code? Oh no no! It seg-faults the Python interpreter.

But in the course of using the class several times and ways in the main window code I found it expedient to keep adding simple functions to it, so as to avoid having to create nonce QFileInfo objects. Here's it's latest incarnation.

class FileBasedTextStream(QTextStream):
    def __init__(self, qfile):
        super().__init__(qfile)
        self.saved_file = qfile
        self.qfi = None # may never need this
    def rewind(self):
        self.seek(0)
    def writeLine(self, str):
        self << str
        self << '\n'
    def basename(self):
        if self.qfi is None:
            self.qfi = QFileInfo(self.saved_file)
        return self.qfi.baseName()
    def filename(self):
        if self.qfi is None:
            self.qfi = QFileInfo(self.saved_file)
        return self.qfi.fileName()
    def filepath(self):
        if self.qfi is None:
            self.qfi = QFileInfo(self.saved_file)
        return self.qfi.absolutePath()

No comments: