我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用sip.wrapinstance()。
def getDock(name='LightingManagerDock'): """ This function creates a dock with the given name. It's an example of how we can mix Maya's UI elements with Qt elements Args: name: The name of the dock to create Returns: QtWidget.QWidget: The dock's widget """ # First lets delete any conflicting docks deleteDock(name) # Then we create a workspaceControl dock using Maya's UI tools # This gives us back the name of the dock created ctrl = pm.workspaceControl(name, dockToMainWindow=('right', 1), label="Lighting Manager") # We can use the OpenMayaUI API to get the actual Qt widget associated with the name qtCtrl = omui.MQtUtil_findControl(ctrl) # Finally we use wrapInstance to convert it to something Python can understand, in this case a QWidget ptr = wrapInstance(long(qtCtrl), QtWidgets.QWidget) # And we return that QWidget back to whoever wants it. return ptr
def qt_import(shi=False, cui=False): """ import pyside/pyQt Returns: multi: QtGui, QtCore, QtWidgets, wrapInstance """ lookup = ["PySide2", "PySide", "PyQt4"] preferredBinding = os.environ.get("MGEAR_PYTHON_QT_BINDING", None) if preferredBinding is not None and preferredBinding in lookup: lookup.remove(preferredBinding) lookup.insert(0, preferredBinding) for binding in lookup: try: return _qt_import(binding, shi, cui) except Exception: pass raise _qt_import("ThisBindingSurelyDoesNotExist", False, False)
def closeEvent(self, event): print 'ui closed' # restore stdout sys.stdout = self.textEdit.stdout_bak sys.stderr = self.textEdit.stderr_bak # ptr = apiUI.MQtUtil.mainWindow() # mwin=sip.wrapinstance(long(ptr), QtGui.QObject) # cmdReporters = cmds.lsUI(type='cmdScrollFieldReporter') # cmdReporter = mwin.findChild(QtGui.QTextEdit, cmdReporters[0]) # sys.stdout=cmdReporter # sys.stderr =cmdReporter event.accept() # event.ignore()
def fix_channelbox_font(): from PyQt4 import QtCore, QtGui import maya.cmds as cmds import maya.OpenMayaUI as mui import sip ptr = mui.MQtUtil.findControl('mainChannelBox') channelBox = sip.wrapinstance(long(ptr), QtCore.QObject) # styleSheet = ''' # QWidget { # /* font-family: "Courier New"; */ # font: normal %spx; # } # ''' % '16' # channelBox.setStyleSheet(styleSheet) channelBox.verticalHeader().setDefaultSectionSize(18)
def __init__(self, title, icon_path): self._title = title self._icon = QtGui.QIcon(icon_path) # IDA 7+ Widgets if using_ida7api: import sip self._form = idaapi.create_empty_widget(self._title) self._widget = sip.wrapinstance(long(self._form), QtWidgets.QWidget) # NOTE: LOL # legacy IDA PluginForm's else: self._form = idaapi.create_tform(self._title, None) if using_pyqt5: self._widget = idaapi.PluginForm.FormToPyQtWidget(self._form) else: self._widget = idaapi.PluginForm.FormToPySideWidget(self._form) self._widget.setWindowIcon(self._icon)
def getMayaMainWindow(): """ Since Maya is Qt, we can parent our UIs to it. This means that we don't have to manage our UI and can leave it to Maya. Returns: QtWidgets.QMainWindow: The Maya MainWindow """ # We use the OpenMayaUI API to get a reference to Maya's MainWindow win = omui.MQtUtil_mainWindow() # Then we can use the wrapInstance method to convert it to something python can understand # In this case, we're converting it to a QMainWindow ptr = wrapInstance(long(win), QtWidgets.QMainWindow) # Finally we return this to whoever wants it return ptr
def _pyqt5(): """Initialise PyQt5""" import PyQt5 as module _setup(module, ["uic"]) try: import sip Qt.QtCompat.wrapInstance = ( lambda ptr, base=None: _wrapinstance( sip.wrapinstance, ptr, base) ) Qt.QtCompat.getCppPointer = lambda object: \ sip.unwrapinstance(object) except ImportError: pass # Optional if hasattr(Qt, "_uic"): Qt.QtCompat.loadUi = _loadUi if hasattr(Qt, "_QtCore"): Qt.__binding_version__ = Qt._QtCore.PYQT_VERSION_STR Qt.__qt_version__ = Qt._QtCore.QT_VERSION_STR Qt.QtCompat.translate = Qt._QtCore.QCoreApplication.translate if hasattr(Qt, "_QtWidgets"): Qt.QtCompat.setSectionResizeMode = \ Qt._QtWidgets.QHeaderView.setSectionResizeMode _reassign_misplaced_members("pyqt5")
def _qt_import(binding, shi=False, cui=False): QtGui = None QtCore = None QtWidgets = None wrapInstance = None if binding == "PySide2": from PySide2 import QtGui, QtCore, QtWidgets import shiboken2 as shiboken from shiboken2 import wrapInstance from pyside2uic import compileUi elif binding == "PySide": from PySide import QtGui, QtCore import PySide.QtGui as QtWidgets import shiboken from shiboken import wrapInstance from pysideuic import compileUi elif binding == "PyQt4": from PyQt4 import QtGui from PyQt4 import QtCore import PyQt4.QtGui as QtWidgets from sip import wrapinstance as wrapInstance from PyQt4.uic import compileUi print("Warning: 'shiboken' is not supported in 'PyQt4' Qt binding") shiboken = None else: raise Exception("Unsupported python Qt binding '%s'" % binding) rv = [QtGui, QtCore, QtWidgets, wrapInstance] if shi: rv.append(shiboken) if cui: rv.append(compileUi) return rv
def _pyqt5(): """Initialise PyQt5""" import PyQt5 as module _setup(module, ["uic"]) try: import sip Qt.QtCompat.wrapInstance = ( lambda ptr, base=None: _wrapinstance( sip.wrapinstance, ptr, base) ) Qt.QtCompat.getCppPointer = lambda object: \ sip.unwrapinstance(object) except ImportError: pass # Optional if hasattr(Qt, "_uic"): Qt.QtCompat.loadUi = _loadUi if hasattr(Qt, "_QtCore"): Qt.__binding_version__ = Qt._QtCore.PYQT_VERSION_STR Qt.__qt_version__ = Qt._QtCore.QT_VERSION_STR Qt.QtCompat.translate = Qt._QtCore.QCoreApplication.translate if hasattr(Qt, "_QtWidgets"): Qt.QtCompat.setSectionResizeMode = \ Qt._QtWidgets.QHeaderView.setSectionResizeMode _reassign_misplaced_members("PyQt5") _build_compatibility_members('PyQt5')
def BT_GetMayaWindow(): ptr = apiUI.MQtUtil.mainWindow() if ptr is not None: if BT_MayaVersionNumber < 2014: return wrapinstance(long(ptr), QtCore.QObject) else: return wrapInstance(long(ptr), QtGui.QWidget)
def getMayaWindow(): 'Get the maya main window as a QMainWindow instance' ptr = mui.MQtUtil.mainWindow() return sip.wrapinstance(long(ptr), QtCore.QObject)
def get_ida_bg_color_ida7(): """ Get the background color of an IDA disassembly view. (IDA 7+) """ names = ["Enums", "Structures"] names += ["Hex View-%u" % i for i in range(5)] names += ["IDA View-%c" % chr(ord('A') + i) for i in range(5)] # find a form (eg, IDA view) to analyze colors from for window_name in names: twidget = idaapi.find_widget(window_name) if twidget: break else: raise RuntimeError("Failed to find donor view") # touch the target form so we know it is populated touch_window(twidget) # locate the Qt Widget for a form and take 1px image slice of it import sip widget = sip.wrapinstance(long(twidget), QtWidgets.QWidget) pixmap = widget.grab(QtCore.QRect(0, 10, widget.width(), 1)) # convert the raw pixmap into an image (easier to interface with) image = QtGui.QImage(pixmap.toImage()) # return the predicted background color return QtGui.QColor(predict_bg_color(image))
def getMayaWindow(): """ Get the main Maya window as a QtGui.QMainWindow instance @return: QtGui.QMainWindow instance of the top level Maya windows """ ptr = apiUI.MQtUtil.mainWindow() if ptr is not None: return sip.wrapinstance(long(ptr), QtCore.QObject)
def toQtObject(mayaName): """ Convert a Maya ui path to a Qt object @param mayaName: Maya UI Path to convert (Ex: "scriptEditorPanel1Window|TearOffPane|scriptEditorPanel1|testButton" ) @return: PyQt representation of that object """ ptr = apiUI.MQtUtil.findControl(mayaName) if ptr is None: ptr = apiUI.MQtUtil.findLayout(mayaName) if ptr is None: ptr = apiUI.MQtUtil.findMenuItem(mayaName) if ptr is not None: return sip.wrapinstance(long(ptr), QtCore.QObject)
def getMayaWindow(self): """ Get the main Maya window as a QtGui.QMainWindow instance @return: QtGui.QMainWindow instance of the top level Maya windows """ ptr = apiUI.MQtUtil.mainWindow() if ptr is not None: return sip.wrapinstance(long(ptr), QObject)
def mui_to_qt(self, mui_name): if hostMode != "maya": return ptr = mui.MQtUtil.findControl(mui_name) if ptr is None: ptr = mui.MQtUtil.findLayout(mui_name) if ptr is None: ptr = mui.MQtUtil.findMenuItem(mui_name) if ptr is not None: if qtMode in (0,2): # ==== for pyside ==== return shiboken.wrapInstance(long(ptr), QtWidgets.QWidget) elif qtMode in (1,3): # ==== for PyQt==== return sip.wrapinstance(long(ptr), QtCore.QObject)
def mui_to_qt(self, mui_name): ptr = mui.MQtUtil.findControl(mui_name) if ptr is None: ptr = mui.MQtUtil.findLayout(mui_name) if ptr is None: ptr = mui.MQtUtil.findMenuItem(mui_name) if ptr is not None: if qtMode == 0: # ==== for pyside ==== return shiboken.wrapInstance(long(ptr), QtGui.QWidget) elif qtMode == 1: # ==== for PyQt==== return sip.wrapinstance(long(ptr), QtCore.QObject)
def main(): parentWin = None app = None if deskMode == 0: if qtMode == 0: # ==== for pyside ==== parentWin = shiboken.wrapInstance(long(mui.MQtUtil.mainWindow()), QtGui.QWidget) elif qtMode == 1: # ==== for PyQt==== parentWin = sip.wrapinstance(long(mui.MQtUtil.mainWindow()), QtCore.QObject) if deskMode == 1: app = QtGui.QApplication(sys.argv) # single UI window code, so no more duplicate window instance when run this function global single_UITranslator if single_UITranslator is None: single_UITranslator = UITranslator(parentWin) # extra note: in Maya () for no parent; (parentWin,0) for extra mode input single_UITranslator.show() if deskMode == 1: sys.exit(app.exec_()) # example: show ui stored print(single_UITranslator.uiList.keys()) return single_UITranslator # If you want to be able to load multiple windows of the same ui, use code below
def getMayaWindow(qt): ptr = omui.MQtUtil.mainWindow() if ptr is not None: if qt: #PyQt return wrp(long(ptr), QObject) else: #PySide return wrp(long(ptr), QMainWindow)
def _pyqt5(): """Initialise PyQt5""" import PyQt5 as module _setup(module, ["uic"]) try: import sip Qt.QtCompat.wrapInstance = ( lambda ptr, base=None: _wrapinstance( sip.wrapinstance, ptr, base) ) Qt.QtCompat.getCppPointer = lambda object: \ sip.unwrapinstance(object) except ImportError: pass # Optional if hasattr(Qt, "_uic"): Qt.QtCompat.loadUi = _loadUi if hasattr(Qt, "_QtCore"): Qt.__binding_version__ = Qt._QtCore.PYQT_VERSION_STR Qt.__qt_version__ = Qt._QtCore.QT_VERSION_STR Qt.QtCompat.qInstallMessageHandler = _qInstallMessageHandler Qt.QtCompat.translate = Qt._QtCore.QCoreApplication.translate if hasattr(Qt, "_QtWidgets"): Qt.QtCompat.setSectionResizeMode = \ Qt._QtWidgets.QHeaderView.setSectionResizeMode _reassign_misplaced_members("PyQt5") _build_compatibility_members('PyQt5')
def get_selected_funcs(): """ Return the list of function names selected in the Functions window. """ # NOTE / COMPAT: if using_ida7api: import sip twidget = idaapi.find_widget("Functions window") widget = sip.wrapinstance(long(twidget), QtWidgets.QWidget) # NOTE: LOL else: tform = idaapi.find_tform("Functions window") if using_pyqt5: widget = idaapi.PluginForm.FormToPyQtWidget(tform) else: widget = idaapi.PluginForm.FormToPySideWidget(tform) # TODO: test this if not widget: idaapi.warning("Unable to find 'Functions window'") return # # locate the table widget within the Functions window that actually holds # all the visible function metadata # table = widget.findChild(QtWidgets.QTableView) # # scrape the selected function names from the Functions window table # selected_funcs = [str(s.data()) for s in table.selectionModel().selectedRows()] # # re-map the scraped names as they appear in the function table, to their true # names as they are saved in the IDB. See the match_funcs(...) function # comment for more details # return match_funcs(selected_funcs)
def main(mode=0): # get parent window in Maya parentWin = None if hostMode == "maya": if qtMode in (0,2): # pyside parentWin = shiboken.wrapInstance(long(mui.MQtUtil.mainWindow()), QtWidgets.QWidget) elif qtMode in (1,3): # PyQt parentWin = sip.wrapinstance(long(mui.MQtUtil.mainWindow()), QtCore.QObject) # create app object for certain host app = None if hostMode in ("desktop", "blender"): app = QtWidgets.QApplication(sys.argv) #-------------------------- # ui instance #-------------------------- # template 1 - Keep only one copy of windows ui in Maya global single_UITranslator if single_UITranslator is None: if hostMode == "maya": single_UITranslator = UITranslator(parentWin, mode) else: single_UITranslator = UITranslator() # extra note: in Maya () for no parent; (parentWin,0) for extra mode input single_UITranslator.show() ui = single_UITranslator # template 2 - allow loading multiple windows of same UI in Maya ''' if hostMode == "maya": ui = UITranslator(parentWin) ui.show() else: # extra note: in Maya () for no parent; (parentWin,0) for extra mode input ''' # loop app object for certain host if hostMode in ("desktop"): sys.exit(app.exec_()) return ui
def main(mode=0): # get parent window in Maya parentWin = None if hostMode == "maya": if qtMode in (0,2): # pyside parentWin = shiboken.wrapInstance(long(mui.MQtUtil.mainWindow()), QtWidgets.QWidget) elif qtMode in (1,3): # PyQt parentWin = sip.wrapinstance(long(mui.MQtUtil.mainWindow()), QtCore.QObject) # create app object for certain host app = None if hostMode in ("desktop", "blender"): app = QtWidgets.QApplication(sys.argv) #-------------------------- # ui instance #-------------------------- # template 1 - Keep only one copy of windows ui in Maya global single_UniversalToolUI if single_UniversalToolUI is None: if hostMode == "maya": single_UniversalToolUI = UniversalToolUI(parentWin, mode) else: single_UniversalToolUI = UniversalToolUI() # extra note: in Maya () for no parent; (parentWin,0) for extra mode input single_UniversalToolUI.show() ui = single_UniversalToolUI # template 2 - allow loading multiple windows of same UI in Maya ''' if hostMode == "maya": ui = UniversalToolUI(parentWin) ui.show() else: # extra note: in Maya () for no parent; (parentWin,0) for extra mode input ''' # loop app object for certain host if hostMode in ("desktop"): sys.exit(app.exec_()) return ui
def main(mode=0): # get parent window in Maya parentWin = None if hostMode == "maya": if qtMode in (0,2): # pyside parentWin = shiboken.wrapInstance(long(mui.MQtUtil.mainWindow()), QtWidgets.QWidget) elif qtMode in (1,3): # PyQt parentWin = sip.wrapinstance(long(mui.MQtUtil.mainWindow()), QtCore.QObject) # create app object for certain host app = None if hostMode in ("desktop", "blender"): app = QtWidgets.QApplication(sys.argv) #-------------------------- # ui instance #-------------------------- # template 1 - Keep only one copy of windows ui in Maya global single_UserClassUI if single_UserClassUI is None: if hostMode == "maya": single_UserClassUI = UserClassUI(parentWin, mode) else: single_UserClassUI = UserClassUI() # extra note: in Maya () for no parent; (parentWin,0) for extra mode input single_UserClassUI.show() ui = single_UserClassUI # template 2 - allow loading multiple windows of same UI in Maya ''' if hostMode == "maya": ui = UserClassUI(parentWin) ui.show() else: # extra note: in Maya () for no parent; (parentWin,0) for extra mode input ''' # loop app object for certain host if hostMode in ("desktop"): sys.exit(app.exec_()) return ui