1. 程式人生 > >maya 自定義視窗置於頂部和不置頂對比以及視窗透明

maya 自定義視窗置於頂部和不置頂對比以及視窗透明

#第一種方法
from PySide2 import QtGui,QtWidgets,QtCore
class OptionWidget(QtWidgets.QWidget):
    closed = QtCore.Signal()

    def __init__(self, *args, **kwargs):
        super(OptionWidget, self).__init__(*args, **kwargs)

        mainLayout =QtWidgets.QVBoxLayout(self)

        button = QtWidgets.QPushButton('Close', self)
        button.clicked.connect(self.__close)
        mainLayout.addWidget(button)
        mainLayout.addStretch(True)


    def __close(self):
        self.closed.emit()

class DraggableWindow(mayaMixin.MayaQWidgetBaseMixin,QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(DraggableWindow, self).__init__(*args, **kwargs)
        #self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        optionWidget = OptionWidget(self)
        optionWidget.closed.connect(self.close)
        self.setCentralWidget(optionWidget)

        statusBar = self.statusBar()

        self.__isDrag = False
        self.__startPos = QtCore.QPoint(0, 0)
        self.resize(200,100)
        self.move(300,300)


    def mousePressEvent(self, event):
        self.__isDrag = True
        self.__startPos = event.pos()
        super(DraggableWindow, self).mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        self.__isDrag = False
        super(DraggableWindow, self).mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.__isDrag:
            self.move(self.mapToParent(event.pos() - self.__startPos))
        super(DraggableWindow, self).mouseMoveEvent(event)


window = DraggableWindow()
window.show()

#第二種方法
from PySide2 import QtGui,QtWidgets,QtCore

class OptionWidget(QtWidgets.QWidget):
    closed = QtCore.Signal()

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

        mainLayout =QtWidgets.QVBoxLayout(self)

        button = QtWidgets.QPushButton('Close', self)
        button.clicked.connect(self.__close)
        mainLayout.addWidget(button)
        mainLayout.addStretch(True)


    def __close(self):
        self.closed.emit()

class DraggableWindow(QtWidgets.QMainWindow):
    parentUI=QtWidgets.QApplication.activeWindow()
    def __init__(self, parent=parentUI):
        super(DraggableWindow, self).__init__(parent)
        #self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        optionWidget = OptionWidget(self)
        optionWidget.closed.connect(self.close)
        self.setCentralWidget(optionWidget)

        statusBar = self.statusBar()

        self.__isDrag = False
        self.__startPos = QtCore.QPoint(0, 0)
        self.resize(200,100)
        self.move(300,300)


    def mousePressEvent(self, event):
        self.__isDrag = True
        self.__startPos = event.pos()
        super(DraggableWindow, self).mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        self.__isDrag = False
        super(DraggableWindow, self).mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.__isDrag:
            self.move(self.mapToParent(event.pos() - self.__startPos))
        super(DraggableWindow, self).mouseMoveEvent(event)


window = DraggableWindow()
window.show()

這裡寫圖片描述

如果自定義視窗不置頂,當對其他的視窗操作時,自定義視窗就會置於底部, 之後,再次使用還要手動調出來,非常不方便。 使用這兩種方法就可以解決這個問題。

其中,#self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

這句話是設定視窗透明的,因為影響截圖,所以註釋了。