import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import math
class page2(QWidget):
def __init__(self):
super(page2,self).__init__()
self.label1 = QLabel(self)
self.label1.setText('aaaapage2')
self.label1.move(50, 50)
def paintEvent(self, event):
self.painter = QPainter(self)
self.painter.drawPixmap(0, 0, QPixmap("./icon/v2.jpeg"))
self.painter.end()
class page1(QWidget):
def __init__(self):
super(page1,self).__init__()
self.label1 = QLabel(self)
self.label1.setText('abcd')
self.label1.move(50,50)
self.sheetStyle = {'color': '#00ffff', 'line_width': 2, 'line_shape': 1, 'shape': 'Line','pattern':0}
def get_signal(self,dict): # 接收来自主程序的变更sheetStyle 信号槽
self.sheetStyle = dict
def page1_draw(self):
self.painter = QPainter(self)
print(self.sheetStyle)
pen = QPen()
pen_shape = [Qt.SolidLine, Qt.DashLine, Qt.DashDotLine, Qt.DotLine, Qt.DashDotDotLine, Qt.CustomDashLine]
pen.setStyle(pen_shape[int(self.sheetStyle['line_shape'])]) # 设置线形
color = QColor()
color.setNamedColor(self.sheetStyle['color']) # #xxxxxx 颜色 转换成 QColor()
pen.setColor(color)
pen.setWidth(int(self.sheetStyle['line_width'])) # 设置线宽
self.painter.setPen(pen)
self.painter.begin(self)
a = int(self.width() / 10)
b = int(self.height() / 10)
rect = QRect(a * 2, 3 * b, a * 6, 4 * b)
startAngle = 30 * 16
spanAngle = 120 * 16
path = QPainterPath()
path.addRect(150, 150, 100, 100)
path.moveTo(100, 100)
path.cubicTo(300, 100, 200, 200, 300, 300) # 三级贝塞尔曲线,前二点为控制点,最后点为最终点
path.cubicTo(100, 300, 200, 200, 100, 100)
# path.closeSubpath(); # 封闭
points = QPolygon([QPoint(150, 100), QPoint(300, 150), QPoint(350, 250), QPoint(100, 300)])
if self.sheetStyle['shape'] == 'Line':
self.painter.drawLine(a, b, a * 9, b * 9)
elif self.sheetStyle['shape'] == 'Points':
for i in range(1000):
x = 200 * (-1 + 2.0 * i / 1000) + self.width() / 2.0 # 绘制函数图像,它的周期是【-100,100】
y = -150 * math.sin((x - self.width() / 2.0) * math.pi / 50) + self.height() / 2.0
self.painter.drawPoint(int(x), int(y))
elif self.sheetStyle['shape'] == 'Polyline':
self.painter.drawPolyline(QPoint(a, b), QPoint(a * 7, b * 2), QPoint(a * 6, b * 8), QPoint(a * 2, b * 6),
QPoint(a * 2, b * 1))
elif self.sheetStyle['shape'] == 'Polygon':
self.painter.drawPolygon(QPoint(a, b), QPoint(a * 7, b * 2), QPoint(a * 6, b * 8), QPoint(a * 2, b * 6),
QPoint(a * 2, b * 1))
elif self.sheetStyle['shape'] == 'Rect':
self.painter.drawRect(rect)
elif self.sheetStyle['shape'] == 'RoundedRect':
self.painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize)
elif self.sheetStyle['shape'] == 'Ellipse':
self.painter.drawEllipse(rect)
elif self.sheetStyle['shape'] == 'Arc':
self.painter.drawArc(rect, 0, 180 * 16)
elif self.sheetStyle['shape'] == 'Chord':
self.painter.drawChord(rect, 16, 60 * 16) # 扇形
elif self.sheetStyle['shape'] == 'Pie':
self.painter.drawPie(rect, 16, 100 * 16)
elif self.sheetStyle['shape'] == 'Path':
self.painter.drawPath(path)
elif self.sheetStyle['shape'] == 'Text':
self.painter.setFont(QFont("Arial", 30))
self.painter.drawText(rect, Qt.AlignCenter, "Hello Qt!")
elif self.sheetStyle['shape'] == 'Pixmap':
self.painter.drawPixmap(0, 0, QPixmap("./icon/v1.jpeg"))
self.painter.end()
def paintEvent(self,event):
self.page1_draw()
class MainWindow(QMainWindow):
signal = pyqtSignal(dict)
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle('QPaint例子')
self.sheetStyle = {'color': '#00ffff', 'line_width': 2, 'line_shape': 1, 'shape': 'Line','pattern':0} #默认表
self.page1 = page1()
self.page2 = page2()
self.btn1 = QPushButton('切换1')
self.btn2 = QPushButton('切换2')
self.btn3 = QPushButton('改变颜色')
self.spin = QSpinBox()
self.combo1 = QComboBox()
self.combo2 = QComboBox()
self.combo3 = QComboBox()
self.stack = QStackedWidget()
self.mainFrame = QWidget()
hbox = QHBoxLayout()
vhbox = QVBoxLayout()
spacerItem = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
shape = ['Line', 'Points', 'Polyline', 'Polygon', 'Rect', 'RoundedRect', 'Ellipse', 'Arc','Chord', 'Pie', 'Path', 'Text', 'Pixmap']
self.combo1.addItems(shape)
self.combo2.addItems(['SolidLine', 'DashLine', 'DashDotLine','DotLine', 'DashDotDotLine', 'CustomDashLine'])
self.combo3.addItems(['NoBrush', 'SolidPattern', 'Dense1Pattern', 'Dense2Pattern','Dense3Pattern', 'Dense4Pattern', 'Dense5Pattern', 'Dense6Pattern','Dense7Pattern', 'HorPattern', 'VerPattern', 'CrossPattern','BDiagPattern', 'FDiagPattern', 'DiagCrossPattern'])
btn3_color = QColor(0,255,255)
self.spin.setMaximum(20)
self.spin.setMinimum(1)
self.btn3.setStyleSheet("QPushButton{ background-color: %s }" % btn3_color.name()) # 设置 frm 颜色为 clo
vhbox.addWidget(self.btn1)
vhbox.addWidget(self.btn2)
vhbox.addWidget(self.btn3)
vhbox.addWidget(self.spin)
vhbox.addItem(spacerItem)
vhbox.addWidget(self.combo1)
vhbox.addWidget(self.combo2)
vhbox.addWidget(self.combo3)
self.stack.setFrameShape(QFrame.Box)
self.stack.setFrameShadow(QFrame.Raised)
self.stack.addWidget(self.page1)
self.stack.addWidget(self.page2)
self.page1.label1.setText('page1')
self.page2.label1.setText('page2')
hbox.addLayout(vhbox)
hbox.addWidget(self.stack)
self.mainFrame.setLayout(hbox)
self.setCentralWidget(self.mainFrame)
#------------------槽信号-------------------------------------------------------------------------
self.btn1.clicked.connect(self.btn1_clicked)
self.btn2.clicked.connect(self.btn2_clicked)
self.btn3.clicked.connect(self.btn3_clicked)
self.spin.valueChanged.connect(self.spin_changed)
self.combo1.currentTextChanged.connect(self.combo1_change)
self.combo2.currentTextChanged.connect(self.combo2_change)
self.combo3.currentTextChanged.connect(self.combo3_change)
self.signal.connect(self.page1.get_signal)
self.update()
def combo1_change(self):
self.sheetStyle['shape'] = self.combo1.currentText()
self.signal.emit(self.sheetStyle)
self.update()
def combo2_change(self):
self.sheetStyle['line_shape'] = self.combo2.currentIndex()
self.signal.emit(self.sheetStyle)
self.update()
def combo3_change(self):
self.sheetStyle['pattern'] = self.combo3.currentIndex()
self.signal.emit(self.sheetStyle)
self.update()
def btn1_clicked(self):
self.stack.setCurrentIndex(1)
def btn2_clicked(self):
self.stack.setCurrentIndex(0)
def btn3_clicked(self):
btn3_color = QColorDialog.getColor()
self.sheetStyle['color'] = btn3_color.name()
self.signal.emit(self.sheetStyle)
self.btn3.autoFillBackground()
self.btn3.setStyleSheet("QPushButton{background-color:%s}"%btn3_color.name())
def spin_changed(self):
self.sheetStyle['line_width'] = self.spin.value()
self.signal.emit(self.sheetStyle)
self.update()
def paintEvent(self,event):
painter = QPainter(self)
pen = QPen(Qt.red,2,Qt.SolidLine)
painter.setPen(pen)
painter.begin(self)
painter.drawLine(0,0,200,200)
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
demo = MainWindow()
demo.show()
sys.exit(app.exec_())
Python学习笔记:PYQT5的QStackWidget多面页例程
于 2022-04-23 23:47:46 首次发布