在python中创建一个QMainWindow,尽管它是可移动的和可调整大小的

问题描述:

我想创建一个没有框架和标题栏的QMainWindow,尽管它是可移动的和可调整大小的我试过self.setWindowFlags(Qt.FramelessWindowHint)但它不能移动或浮动。在python中创建一个QMainWindow,尽管它是可移动的和可调整大小的

我不明白为什么你想要你想要的东西......我认为,既然你没有窗口标题,你想通过点击窗口区域内的任何点来拖动窗口,然后用鼠标拖动。请注意,如果窗口包含子控件也反应按下鼠标和移动事件,这可能是糟糕的主意......

但是这是基本的解决方案:

from PyQt4.QtCore import Qt 
from PyQt4.QtGui import QApplication, QMainWindow 

class MainWindow(QMainWindow): 

    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setWindowFlags(Qt.FramelessWindowHint) 

    def mousePressEvent(self, event): 
     # Store the positions of mouse and window and 
     # change the window position relative to them. 
     self.windowPos = self.pos() 
     self.mousePos = event.globalPos() 
     super(MainWindow, self).mousePressEvent(event) 

    def mouseMoveEvent(self, event): 
     self.move(self.windowPos + event.globalPos() - self.mousePos) 
     super(MainWindow, self).mouseMoveEvent(event) 

app = QApplication([]) 
wnd = MainWindow() 
wnd.show() 
app.exec_()