简体   繁体   English

对Qt无框架小部件实施“调整大小”选项

[英]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. 我也刚刚遇到了这个问题,并通过为QMainWindow添加自定义mouseEvent处理程序解决了该问题。 I'm using PyQt, but it should be fairly similar in C++. 我正在使用PyQt,但在C ++中应该相当相似。

In my implementation, dragging the right mouse button anywhere on the frameless widget (called MyClass ) resizes it. 在我的实现中,将鼠标右键拖动到无框架小部件(称为MyClass )上的任意位置将调整其大小。

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. 如果在按下按钮时(即,拖动鼠标时)移动了鼠标,请调整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. 释放鼠标按钮后,将按钮变量重置为False即可停止调整大小。

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

Use a QSizeGrip 使用QSizeGrip

The QSizeGrip class provides a resize handle for resizing top-level windows. QSizeGrip类提供用于调整顶级窗口大小的调整大小句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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