QProgressDialog

在這裏插入圖片描述

# -*- coding: utf-8 -*-
"""
Author: Administrator
Date: 2020/5/12 23:27
"""

from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 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 Example"
    
    dlg_instance = None
    def __init__(self, parent=maya_main_window()):
        super(CustomDialog, self).__init__(parent)

        self.setWindowTitle(self.WINDOW_TITLE)
        self.setMinimumSize(600, 400)
        self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)

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

    def create_widgets(self):
        self.bttn = QtWidgets.QPushButton("Just Do It")

    def create_layout(self):
        bttn_layout = QtWidgets.QHBoxLayout()
        bttn_layout.addStretch()
        bttn_layout.addWidget(self.bttn)

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

    def create_connections(self):
        self.bttn.clicked.connect(self.run_progress)

    def run_progress(self):
        num = 10

        progress_dialog = QtWidgets.QProgressDialog("Waiting to process...", "Cancel", 0, num, self)
        progress_dialog.setWindowTitle("Progress...")
        progress_dialog.setValue(0)
        progress_dialog.setWindowModality(QtCore.Qt.WindowModal)
        progress_dialog.show()

        QtCore.QCoreApplication.processEvents()

        for i in range(1, num + 1):
            if progress_dialog.wasCanceled():
                break

            progress_dialog.setLabelText("Processing operation: {0} (of {1})".format(i, num))
            progress_dialog.setValue(i)
            time.sleep(0.5)

            QtCore.QCoreApplication.processEvents()

        progress_dialog.close()
    
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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章