萬能Qt

萬能Qt,不再糾結PyQt4/PyQt5/PySide/PySide2
下載地址:https://github.com/mottosso/Qt.py

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

在Maya內導入模塊即可,注意把shiboken2模塊換成Qt.QtCompat,如:

from Qt import QtCore
from Qt import QtWidgets
from Qt.QtCompat import wrapInstance

拿上個ProgressBar爲例:

# -*- coding: utf-8 -*-

"""
Author: Administrator
Date: 2020/5/14 22:41
"""

from Qt import QtCore
from Qt import QtWidgets
from Qt.QtCompat import wrapInstance

import maya.OpenMayaUI as omui
import maya.cmds as cmds
import maya.mel as mel

import time

def maya_main_window():
    """
    Return the Maya main window widget as a Python object
    """
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)

class CustomDialog(QtWidgets.QDialog):
    WINDOW_TITLE = "Progress Bar"
    
    dlg_instance = None
    def __init__(self, parent=maya_main_window()):
        super(CustomDialog, self).__init__(parent)

        self.setWindowTitle(self.WINDOW_TITLE)
        self.setMinimumSize(300, 100)
        self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)

        self.cancel_progress = False

        self.create_widgets()
        self.create_layout()
        self.create_connections()

    def create_widgets(self):
        self.progress_bar_label = QtWidgets.QLabel("Progress waiting...")
        self.progress_bar = QtWidgets.QProgressBar()
        self.progress_bar.setValue(0)
        self.progress_bar.setRange(0, 10)

        self.run_progress_bttn = QtWidgets.QPushButton("Do It")
        self.cancel_bttn = QtWidgets.QPushButton("Cancel")

        self.update_visibility()

    def create_layout(self):
        progress_layout = QtWidgets.QVBoxLayout()
        progress_layout.addWidget(self.progress_bar_label)
        progress_layout.addWidget(self.progress_bar)

        bttn_layout = QtWidgets.QHBoxLayout()
        bttn_layout.addStretch()
        bttn_layout.addWidget(self.run_progress_bttn)
        bttn_layout.addWidget(self.cancel_bttn)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.addLayout(progress_layout)
        main_layout.addLayout(bttn_layout)

    def create_connections(self):
        self.run_progress_bttn.clicked.connect(self.run_progress_bar)
        self.cancel_bttn.clicked.connect(self.canel)

    def run_progress_bar(self):

        self.cancel_progress = True
        self.update_visibility()
        num = 10

        for i in range(1, num + 1):
            if not self.cancel_progress:
                break

            self.progress_bar_label.setText("Progress {0}/{1}".format(i, num))
            self.progress_bar.setValue(i)
            time.sleep(0.5)

            QtCore.QCoreApplication.processEvents()

        self.cancel_progress = False
        self.update_visibility()

    def canel(self):
        self.cancel_progress = False

    def update_visibility(self):
        self.run_progress_bttn.setVisible(not self.cancel_progress)
        self.progress_bar.setVisible(self.cancel_progress)
        self.progress_bar_label.setVisible(self.cancel_progress)
        self.cancel_bttn.setVisible(self.cancel_progress)
    
def showWindow():
    global dialog
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = CustomDialog()
    dialog.show()

if __name__ == "__main__":
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = CustomDialog()
    dialog.show()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章