一半君的总结纸

听话只听一半君

如何在QMainWindow的statusBar显示statusTip而不是Maya左下角处显示

QMainWindow的statusBar可以显示ui上按钮的提示,可是在maya里总是会显示到Maya左下角的总的statusBar那儿去

下面的方法属于硬上,给每个有toolTip的widget都安装一个eventfilter,有statusTip就用他,没有的话就用toolTip当statusTip

from shiboken2 import wrapInstance
import maya.OpenMayaUI as apiUI
import os
import sys
import traceback
from PySide2 import QtCore, QtGui, QtWidgets, QtUiTools
from PySide2.QtUiTools import QUiLoader
import shiboken2
uiFile = os.path.join(os.path.dirname(__file__), 'qt_example_mainwindow.ui')


def getMayaWindow():
    ptr = apiUI.MQtUtil.mainWindow()
    return wrapInstance(long(ptr), QtWidgets.QWidget)


class Manager(QtCore.QObject):
    _instance = None

    def __init__(self, parent=None):
        super(Manager, self).__init__(parent)

        ui_file = QtCore.QFile(uiFile)
        ui_file.open(QtCore.QFile.ReadOnly)
        loader = QtUiTools.QUiLoader()

        self.ui = loader.load(ui_file, parent)
        ui_file.close()
        # access ui elements with self.ui.pushButton
        self.filter_ = StatusBarEventFilter(self.ui.statusBar())
        for w in self.ui.findChildren(QtWidgets.QWidget):
            if hasattr(w, 'toolTip'):
                w.installEventFilter(self.filter_)

    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls._instance or not shiboken2.isValid(cls._instance):
            cls._instance = Manager(*args, **kwargs)
        cls._instance.ui.show()
        cls._instance.ui.raise_()
        return cls._instance

class StatusBarEventFilter(QtCore.QObject):
    def __init__(self, statusBar):
        QtCore.QObject.__init__(self)
        self.statusBar = statusBar

    def eventFilter(self, source, event):
        event_type = event.type() if isinstance(event, QtCore.QEvent) else event
        if event_type == QtCore.QEvent.Enter:
            toolTip = source.toolTip()
            statusTip = source.statusTip()
            tip = statusTip if statusTip else toolTip
            if not tip:
                return False
            self.statusBar.showMessage(tip)

        elif event_type == QtCore.QEvent.Leave:
            self.statusBar.clearMessage()

        return False

def main():
    win = Manager.instance(getMayaWindow())


if __name__ == '__main__':
    main()

文件链接:
qt_example_mainwindow.ui

发表评论

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

WordPress.com 徽标

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

Google photo

You are commenting using your Google 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 /  更改 )

Connecting to %s

%d 博主赞过: