我在PyQt4中制作了一个非常好的QWidget,但是我想要有圆边,而不是尖锐的边缘,是否可以在pyqt中做到?
from PyQt4 import QtGui, QtCore
import sys
class ControlBar(QtGui.QWidget):
""" docstring for ControlBar"""
def __init__(self, parent=None):
super(ControlBar, self).__init__(parent)
self.resize(440, 100)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setWindowTitle("QLinearGradient Vertical Gradient ")
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.buildUi()
def mousePressEvent(self, event):
self.offset = event.pos()
def mouseMoveEvent(self, event):
try:
x=event.globalX()
y=event.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x-x_w, y-y_w)
except: pass
def paintEvent(self, ev):
painter = QtGui.QPainter(self)
gradient = QtGui.QLinearGradient(QtCore.QRectF(self.rect()).topLeft(),QtCore.QRectF(self.rect()).bottomLeft())
gradient.setColorAt(0.0, QtCore.Qt.black)
gradient.setColorAt(0.4, QtCore.Qt.gray)
gradient.setColorAt(0.7, QtCore.Qt.black)
painter.setBrush(gradient)
painter.drawRoundedRect(0, 0, 440, 100, 20.0, 20.0)
def buildUi(self):
self.hoelayout = QtGui.QHBoxLayout()
self.openBtn = RoundEdgeButton("Hello")
self.backBtn = RoundEdgeButton()
self.pausBtn = RoundEdgeButton()
self.nextBtn = RoundEdgeButton()
self.hoelayout.addStretch(1)
self.hoelayout.addWidget(self.openBtn)
self.hoelayout.addStretch(1)
self.hoelayout.addWidget(self.backBtn)
self.hoelayout.addStretch(1)
self.hoelayout.addWidget(self.pausBtn)
self.hoelayout.addStretch(1)
self.hoelayout.addWidget(self.nextBtn)
self.hoelayout.addStretch(1)
self.setLayout(self.hoelayout)
class RoundEdgeButton(QtGui.QPushButton):
""" docstring for RoundEdgeButton"""
def __init__(self,text=None, parent=None):
super(RoundEdgeButton, self).__init__(parent)
if text:
self.setText(text)
def paintEvent(self, ev):
btnPaint = QtGui.QPainter(self)
btnGradient = QtGui.QLinearGradient(QtCore.QRectF(self.rect()).topLeft(), QtCore.QRectF(self.rect()).bottomLeft())
btnGradient.setColorAt(0.8, QtCore.Qt.black)
btnGradient.setColorAt(0.1, QtCore.Qt.gray)
btnGradient.setColorAt(0.8, QtCore.Qt.black)
btnPaint.setBrush(btnGradient)
btnPaint.drawRoundedRect(self.rect(), 100.0, 100.0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = ControlBar()
win.show()
win.raise_()
sys.exit(app.exec_())
以下是现在的样子
答案 0 :(得分:3)
您可以使用QPainter.drawRoundedRect
:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class MyRoundWidget(QWidget):
def __init__(self, master=None):
super(MyRoundWidget,self).__init__(master)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowTitle("QLinearGradient Vertical Gradient ")
self.setAttribute(Qt.WA_TranslucentBackground)
def paintEvent(self, ev):
painter = QPainter(self)
painter.begin(self)
gradient = QLinearGradient(QRectF(self.rect()).topLeft(),QRectF(self.rect()).bottomLeft())
gradient.setColorAt(0.0, Qt.black)
gradient.setColorAt(0.4, Qt.gray)
gradient.setColorAt(0.7, Qt.black)
painter.setBrush(gradient)
painter.drawRoundedRect(self.rect(), 10.0, 10.0)
painter.end()
def main():
app = QApplication(sys.argv)
widget = MyRoundWidget()
widget.show()
widget.raise_()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
为了使按钮半透明,请执行以下操作:
self.setStyleSheet("""
QPushButton{
opacity: 50;
background-color: rgba(204,204,204,10);
}
""")
在10
和0
之间更改255
您想要的值。
如果您只想要圆形按钮(不是圆形矩形按钮),请将btnPaint.drawRoundedRect(self.rect(), 100.0, 100.0)
替换为btnPaint.drawEllipse(10,10,self.width()-10, self.height()-10)
答案 1 :(得分:2)
画家= QPainter(个体经营) 如果构造QPainter对象,则不必调用begin()和end()。