#第一种方法
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 = Falseself.__startPos = QtCore.QPoint(0, 0)self.resize(200,100)self.move(300,300)def mousePressEvent(self, event):self.__isDrag = Trueself.__startPos = event.pos()super(DraggableWindow, self).mousePressEvent(event)def mouseReleaseEvent(self, event):self.__isDrag = Falsesuper(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,QtCoreclass 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 = Falseself.__startPos = QtCore.QPoint(0, 0)self.resize(200,100)self.move(300,300)def mousePressEvent(self, event):self.__isDrag = Trueself.__startPos = event.pos()super(DraggableWindow, self).mousePressEvent(event)def mouseReleaseEvent(self, event):self.__isDrag = Falsesuper(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)
这句话是设置窗口透明的,因为影响截图,所以注释了。