【问题标题】:Create a QMainWindow in python without frame despite that it is movable and resizable在没有框架的python中创建一个QMainWindow,尽管它是可移动和可调整大小的
【发布时间】:2017-07-16 12:17:46
【问题描述】:

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

【问题讨论】:

    标签: python pyqt4 qmainwindow


    【解决方案1】:

    我不明白你为什么想要你想要的东西......我假设,因为你没有窗口标题,你想通过单击窗口区域内的任意点然后拖动来拖动窗口用鼠标。请注意,如果窗口包含对鼠标按下和移动事件也有反应的子小部件,这可能是个坏主意...

    但这是基本的解决方案:

    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_()
    

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2018-03-05
      • 2021-12-01
      • 1970-01-01
      • 2022-01-10
      • 2011-11-02
      • 2012-11-22
      • 2011-04-20
      • 1970-01-01
      相关资源
      最近更新 更多