实现调整大小选项Qt无框小部件 [英] Implement Resize option to Qt Frameless widget
问题描述
我刚刚遇到这个问题以及,我解决了它通过添加自定义mouseEvent处理程序为我的QMainWindow。我在使用PyQt,但它应该是相当类似的C ++。
在我的实现中,拖动鼠标右键在无框窗口小部件(<$ c $
当鼠标右键被按下时,存储坐标:
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 b $ b self.currenty = self.height()
self.rightClick = True
如果在按钮仍然被按下时(即,当它被拖动时)鼠标移动,则调整QMainWindow的大小。
def mouseMoveEvent(self,event):
super(Myclass,self).mouseMoveEvent(event)
如果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)
当鼠标按钮释放时,将按钮变量重置为False以停止调整大小。
def mouseReleaseEvent(self,event):
super(MyClass,self).mouseReleaseEvent(event)
self.rightClick = False
How can i implement resize option to Qt frameless widget that it's used as Main Window?
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
这篇关于实现调整大小选项Qt无框小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!