pyside2 widget界面去除边框后 添加放大 缩小 关闭按钮并可以鼠标拖动界面移动 鼠标控制界面上行左右伸缩的代码实现
时间: 2023-09-03 17:08:50 浏览: 125
你可以使用以下代码实现:
```python
from PySide2.QtCore import Qt
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QPushButton
class CustomTitleBar(QWidget):
def __init__(self, parent=None):
super(CustomTitleBar, self).__init__(parent)
self.setWindowTitle("CustomTitleBar")
self.setFixedHeight(30)
self.setContentsMargins(0, 0, 0, 0)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.title_label = QLabel(self)
self.title_label.setAlignment(Qt.AlignCenter)
self.title_label.setText("CustomTitleBar")
layout.addWidget(self.title_label)
# Add minimize button
self.minimize_button = QPushButton(self)
self.minimize_button.setIcon(QIcon("minimize.png"))
self.minimize_button.setFixedSize(30, 30)
self.minimize_button.clicked.connect(self.parent().showMinimized)
layout.addWidget(self.minimize_button)
# Add maximize button
self.maximize_button = QPushButton(self)
self.maximize_button.setIcon(QIcon("maximize.png"))
self.maximize_button.setFixedSize(30, 30)
self.maximize_button.clicked.connect(self.toggleMaximized)
layout.addWidget(self.maximize_button)
# Add close button
self.close_button = QPushButton(self)
self.close_button.setIcon(QIcon("close.png"))
self.close_button.setFixedSize(30, 30)
self.close_button.clicked.connect(self.parent().close)
layout.addWidget(self.close_button)
def toggleMaximized(self):
if self.parent().isMaximized():
self.parent().showNormal()
self.maximize_button.setIcon(QIcon("maximize.png"))
else:
self.parent().showMaximized()
self.maximize_button.setIcon(QIcon("restore.png"))
class CustomWidget(QWidget):
def __init__(self):
super(CustomWidget, self).__init__()
self.setWindowTitle("CustomWidget")
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.title_bar = CustomTitleBar(self)
layout.addWidget(self.title_bar)
# Add your content widgets here
# ...
self.show()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.drag_position)
event.accept()
def resizeEvent(self, event):
super(CustomWidget, self).resizeEvent(event)
# Update the title bar width to match the widget width
self.title_bar.setFixedWidth(self.width())
def paintEvent(self, event):
super(CustomWidget, self).paintEvent(event)
# Draw a border around the widget
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 1))
painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
```
这个代码创建了一个自定义窗口,其中包含了一个自定义的标题栏和一个可以自由放置的内容部件。标题栏中包含了最小化、最大化、还原和关闭按钮,可以拖动窗口移动,鼠标控制窗口上下左右伸缩。
需要注意的是,为了使窗口可以自由伸缩,需要在 `resizeEvent` 中更新标题栏的宽度,否则标题栏会超出窗口边界。同时,为了使窗口可以拖动,需要在 `mousePressEvent` 和 `mouseMoveEvent` 中实现拖动逻辑。窗口的边框可以在 `paintEvent` 中绘制出来,这里使用了 `QPainter` 类。
阅读全文