简体   繁体   中英

Implement Resize option to Qt Frameless widget

我如何实现Qt无框架小部件用作主窗口的调整大小选项?

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 (ie, 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

Use a QSizeGrip

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM