PyQt5 to PySide2, loading UI-Files in different classes

后端 未结 3 666
后悔当初
后悔当初 2021-01-14 10:49

I have a python application which runs under python3.6 and is using PyQt5 for loading Ui windows. These windows were created with Qt Designer 5.9.4. The Code below shows a w

3条回答
  •  借酒劲吻你
    2021-01-14 11:22

    PySide does not offer these methods, but one solution is to modify the source code of the PyQt uic module by changing the imports from PyQt5 to PySide2, for legal terms do not modify the license, in addition to the code that will keep the PyQt licenses.

    To do this, download the source code from the following link and unzip it.

    And execute the following script:

    convert_pyqt5_to_pyside2.py

    import os
    import fileinput
    import argparse
    import shutil
    
    def modify_source_code(directory, text_to_search, replacement_text):
        for path, subdirs, files in os.walk(directory):
            for name in files:
                filename = os.path.join(path, name)
                with fileinput.FileInput(filename, inplace=True) as file:
                    for line in file:
                        if line.startswith('#'):
                            # not change on comments
                            print(line, end='')
                        else:
                            print(line.replace(text_to_search, replacement_text), end='')
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("-i", "--input", help="Input directory")
        parser.add_argument("-o", "--output", help="Output directory")
        args = parser.parse_args()
        if args.input and args.output:
            input_dir = os.path.join(os.path.abspath(args.input), "pyuic", "uic")
            output_dir = os.path.abspath(args.output)
            shutil.copytree(input_dir, output_dir)
            modify_source_code(output_dir, 'PyQt5', 'PySide2')
    
    if __name__ == '__main__':
        main()
    

    Using the following command:

    python convert_pyqt5_to_pyside2.py -i /path/of/PyQt5-folder -o fakeuic
    

    Then you can use the loadUiType method from fakeuic:

    from fakeuic import loadUiType  
    from PySide2 import QtCore, QtGui, QtWidgets
    
    Ui_FirstWindow, QFirstWindow = loadUiType('first_window.ui')
    Ui_SecondWindow, QSecondWindow = loadUiType('second_window.ui')
    
    class First(QFirstWindow, Ui_FirstWindow):
        def __init__(self):  
            super(First, self).__init__()
            self.setupUi(self)
            self.button.clicked.connect(self.show_second_window)
    
        def show_second_window(self):
            self.Second = Second()
            self.Second.show()
    
    class Second(QSecondWindow, Ui_SecondWindow):
        def __init__(self):  
            super(Second, self).__init__()
            self.setupUi(self)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        main = First()
        main.show()
        sys.exit(app.exec_())
    

    You can find the complete example here

提交回复
热议问题