def importXmlDOM(filename): ''' The QDomDocument class represents the entire XML document The DOM classes that will be used most often are QDomNode , QDomDocument , QDomElement and QDomText The DOM tree might end up reserving a lot of memory if the XML document is big. For such documents, the QXmlStreamReaderor the QXmlQuery classes might be better solutions. :param filename: filename_path :return: document ''' document = QDomDocument() error = None file = None try: # instantiate a device(QFile) specified by filename file = QFile(filename) if not file.open(QIODevice.ReadOnly): raise IOError(str(file.errorString())) # setContent parses an XML file and creates the DOM tree that represents the document if not document.setContent(file): raise ValueError("could not parse XML") except (IOError, OSError, ValueError) as e: error = "Failed to import: {0}".format(e) finally: if file is not None: file.close() if error is not None: print(error) return document
def load_ui(name, custom_widgets=[], parent=None): loader = QUiLoader() for cw in custom_widgets: loader.registerCustomWidget(cw) path = os.path.join(os.path.dirname(__file__), "ui", name) ui_file = QFile(path) if not ui_file.open(QFile.ReadOnly): logging.critical("Cannot open {}: {}".format(path, ui_file.errorString())) sys.exit(-1) ui = loader.load(ui_file, parent) ui_file.close() return ui
def loadFile(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning( self, "MateWriter", "Cannot read file %s:\n%s." % (fileName, file.errorString())) return inf = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) self.textEdit.setPlainText(inf.readAll()) QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.statusBar().showMessage("File loaded", 2000)
def saveFile(self, fileName): file = QFile(fileName) if not file.open(QFile.WriteOnly | QFile.Text): QMessageBox.warning( self, "MateWriter", "Cannot write file %s:\n%s." % (fileName, file.errorString())) return False outf = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) outf << self.textEdit.toPlainText() QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.statusBar().showMessage("File saved", 2000) return True
def loadFile(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning( self, "MDI", "Cannot read file %s:\n%s." % (fileName, file.errorString())) return False instr = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) self.setPlainText(instr.readAll()) QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.document().contentsChanged.connect(self.documentWasModified) return True
def save(self): filename, _ = QFileDialog.getSaveFileName(self, "Choose a file name", '.', "HTML (*.html *.htm)") if not filename: return file = QFile(filename) if not file.open(QFile.WriteOnly | QFile.Text): QMessageBox.warning( self, "Dock Widgets", "Cannot write file %s:\n%s." % (filename, file.errorString())) return out = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) out << self.textEdit.toHtml() QApplication.restoreOverrideCursor() self.statusBar().showMessage("Saved '%s'" % filename, 2000)
def __init__(self): QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts) app = QApplication(sys.argv) app.setStyle(QStyleFactory.create('Fusion')) ui_file_name = '%s.ui' % Path(__file__).stem ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print('Cannot open %s: %s' % (ui_file_name, ui_file.errorString())) sys.exit(-1) loader = QUiLoader() self.window = loader.load(ui_file) ui_file.close() if not self.window: print(loader.errorString()) sys.exit(-1) self.connect() self.setting() self.window.show() sys.exit(app.exec())
"""QUiLoader example, showing how to dynamically load a Qt Designer form from a UI file.""" from argparse import ArgumentParser, RawTextHelpFormatter import sys from PySide6.QtCore import Qt, QFile, QIODevice from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtUiTools import QUiLoader if __name__ == '__main__': arg_parser = ArgumentParser(description="QUiLoader example", formatter_class=RawTextHelpFormatter) arg_parser.add_argument('file', type=str, help='UI file') args = arg_parser.parse_args() ui_file_name = args.file app = QApplication(sys.argv) ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString())) sys.exit(-1) loader = QUiLoader() widget = loader.load(ui_file, None) ui_file.close() if not widget: print(loader.errorString()) sys.exit(-1) widget.show() sys.exit(app.exec_())