Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
496 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...