pyqt5自定义组件pushbottom
时间: 2023-07-21 19:36:04 浏览: 89
PyQt5实现拖放功能
好的,下面是创建一个自定义的PushBottom组件的步骤:
1. 导入必要的模块
```python
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt
```
2. 创建一个自定义的PushBottom类
```python
class PushBottom(QPushButton):
def __init__(self, parent=None):
super(PushBottom, self).__init__(parent)
```
3. 重写绘制事件
```python
def paintEvent(self, e):
super().paintEvent(e)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
if self.isDown():
painter.setBrush(QColor(255, 0, 0, 255))
else:
painter.setBrush(QColor(255, 0, 0, 180))
painter.drawRoundedRect(self.rect(), 10, 10)
painter.setPen(Qt.white)
painter.setFont(QFont('Arial', 14))
text_width = painter.fontMetrics().width(self.text())
text_height = painter.fontMetrics().height()
painter.drawText(self.rect().center().x() - text_width/2,
self.rect().center().y() + text_height/4,
self.text())
```
4. 添加测试代码
```python
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
btn = PushBottom('Click Me')
btn.resize(120, 60)
btn.show()
sys.exit(app.exec_())
```
这样就可以创建一个简单的自定义PushBottom组件了。您可以根据自己的需求修改绘制事件,以实现更加个性化的效果。
阅读全文