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

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

c++ - Implement Resize option to Qt Frameless widget

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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...