O. Shehab
O. Shehab

Reputation: 81

Create a QMainWindow in python without frame despite that it is movable and resizable

I want to create a QMainWindow without the frame and the titlebar despite that it is movable and resizable I tried self.setWindowFlags(Qt.FramelessWindowHint)but the it is not movable or floatable.

Upvotes: 0

Views: 565

Answers (1)

I do not get the point of why you want what you want... I assume that, since you do not have the window title, you want to drag the window by clicking at any point within the window area and then dragging with the mouse. Note that this might be bad idea if the window contains child widgets which also react to mouse press and move events...

But this is the basic solution:

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

Upvotes: 1

Related Questions