一半君的总结纸

听话只听一半君

2017以后的近现代UI写法总结纸 和Qt.py

2017开始有了Qt5


写法和以前(PyQt4时代)基本一样,只是widgets们都移到了QtWidgets里,下面是之前的例子改成PyQt5的版本

#!/usr/bin/env python2.7
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic
 
uiFile = os.path.join(os.path.dirname(__file__), 'pyqt_example.ui')
 
try:
    import maya.OpenMayaUI as apiUI
    import sip
 
    def getMayaWindow():
        ptr = apiUI.MQtUtil.mainWindow()
        return sip.wrapinstance(long(ptr), QtCore.QObject)
except:
    pass
 
 
class MyWindow(QtWidgets.QDialog):
 
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.ui = uic.loadUi(uiFile, self)
        self.ui.show()
 
 
def main():
    if str(QtWidgets.qApp.applicationName()).startswith('Maya'):
        global win
        try:
            win.close()
        except:
            pass
        win = MyWindow(getMayaWindow())
    else:
        app = QtWidgets.QApplication(sys.argv)
        win = MyWindow()
        sys.exit(app.exec_())
 
if __name__ == "__main__":
        main()

如果有大量代码要改成pyqt5,可以试试
https://github.com/rferrazz/pyqt4topyqt5
或者考虑下增强下python的 2to3 script

Qt.py

而现在由于同一份代码需要在各个软件里运行,大神们想出了各种方法,比如ilm的硬上法,让所有工具都在外部运行,用socket和软件通讯,这样就不必担心所有工具里的qt版本不同了,比如他们的creatureOps虽然是Pyqt4写的,但是在maya2017照样不须任何改动直接运行,另一大门派是Qt.py

#!/usr/bin/env python2.7
import os
import sys
from Qt import QtWidgets, QtCompat

uiFile = os.path.join(os.path.dirname(__file__), 'pyqt_example.ui')
 
try:
    import maya.OpenMayaUI as apiUI
 
    def getMayaWindow():
        ptr = apiUI.MQtUtil.mainWindow()
        return QtCompat.wrapInstance(long(ptr), QtWidgets.QMainWindow)
except:
    pass
 
 
class MyWindow(QtWidgets.QDialog):
 
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.ui = QtCompat.loadUi(uiFile)
        self.ui.show()
 
 
def main():
    qApp = QtWidgets.QApplication.instance()
    if qApp and qApp.applicationName().startswith('Maya'):
        global win
        try:
            win.close()
        except:
            pass
        win = MyWindow(getMayaWindow())
    else:
        app = QtWidgets.QApplication(sys.argv)
        win = MyWindow()
        sys.exit(app.exec_())
 
if __name__ == "__main__":
        main()

如果是在公司环境,可以通过设置QT_PREFERRED_BINDING环境变量来决定优先用PySide2 PyQt5 PySide PyQt4 中的哪一个

链接:
pyqt_example.ui

参考:
https://github.com/mottosso/Qt.py
Developing with Qt.py

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: