Eagle Eye
Eagle Eye

Reputation: 101

Implement Resize option to Qt Frameless widget

How can i implement resize option to Qt frameless widget that it's used as Main Window?

Upvotes: 2

Views: 2196

Answers (2)

baysmith
baysmith

Reputation: 5202

Use a QSizeGrip

The QSizeGrip class provides a resize handle for resizing top-level windows.

Upvotes: 2

Junuxx
Junuxx

Reputation: 14261

I just encountered this problem as well, and I solved it by adding custom mouseEvent handlers for my QMainWindow. I'm using PyQt, but it should be fairly similar in C++.

In my implementation, dragging the right mouse button anywhere on the frameless widget (called MyClass) resizes it.

When the right mouse button is pressed, store the coordinates:

def mousePressEvent(self, event):
    super(MyClass, self).mousePressEvent(event)

    if event.button() == QtCore.Qt.RightButton:
        self.rdragx = event.x()
        self.rdragy = event.y()        
        self.currentx = self.width()
        self.currenty = self.height()
        self.rightClick = True

If the mouse is moved while the button is still pressed (i.e., when it's dragged), resize the QMainWindow. Don't allow it to become smaller than the predefined minimum size.

def mouseMoveEvent(self, event):
    super(Myclass, self).mouseMoveEvent(event)
    if self.rightClick == True:
        x = max(frame.minimumWidth(), 
                self.currentx + event.x() - self.rdragx)
        y = max(frame.minimumHeight(), 
                self.currenty + event.y() - self.rdragy)
        self.resize(x, y)

When the mouse button is released, reset the button variable to False to stop resizing on movement.

def mouseReleaseEvent(self, event):
    super(MyClass, self).mouseReleaseEvent(event)
    self.rightClick = False

Upvotes: 3

Related Questions