完成一个简单的对话框,在对话框中能够选择不同形状进行绘制,并其能够任意绘制简单的图形,并实现对图形的填充、边线及画笔模式的选择。 1.使用QPainter提供的draw函数绘制各种图形 2.使用QPen、QBrush实现画笔及画刷的模式设置
时间: 2024-03-25 16:40:20 浏览: 92
利用QPainter绘制各种图形
3星 · 编辑精心推荐
好的,以下是一个简单的代码示例,可以实现您所需的对话框。
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QCheckBox, QColorDialog
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor
from PyQt5.QtCore import Qt, QPoint
class DrawWidget(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.shapes = ["Rectangle", "Ellipse", "Line"]
self.shape = self.shapes[0]
self.fill = False
self.fill_color = Qt.white
self.border_color = Qt.black
self.border_width = 1
self.pen_style = Qt.SolidLine
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(self.border_color, self.border_width, self.pen_style))
if self.fill:
painter.setBrush(QBrush(self.fill_color))
else:
painter.setBrush(Qt.NoBrush)
if self.shape == "Rectangle":
painter.drawRect(self.rect())
elif self.shape == "Ellipse":
painter.drawEllipse(self.rect())
elif self.shape == "Line":
painter.drawLine(QPoint(0, 0), QPoint(self.width(), self.height()))
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_pos = event.pos()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
end_pos = event.pos()
painter = QPainter(self)
painter.setPen(QPen(self.border_color, self.border_width, self.pen_style))
if self.fill:
painter.setBrush(QBrush(self.fill_color))
else:
painter.setBrush(Qt.NoBrush)
if self.shape == "Rectangle":
painter.drawRect(self.get_rect(self.start_pos, end_pos))
elif self.shape == "Ellipse":
painter.drawEllipse(self.get_rect(self.start_pos, end_pos))
elif self.shape == "Line":
painter.drawLine(self.start_pos, end_pos)
def get_rect(self, start_pos, end_pos):
x = min(start_pos.x(), end_pos.x())
y = min(start_pos.y(), end_pos.y())
w = abs(start_pos.x() - end_pos.x())
h = abs(start_pos.y() - end_pos.y())
return QRect(x, y, w, h)
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.draw_widget = DrawWidget(self)
shape_label = QLabel("Shape:")
self.shape_combo_box = QComboBox()
self.shape_combo_box.addItems(self.draw_widget.shapes)
self.shape_combo_box.currentIndexChanged.connect(self.shape_changed)
fill_check_box = QCheckBox("Fill")
fill_check_box.stateChanged.connect(self.fill_changed)
fill_color_button = QLabel()
fill_color_button.setFixedSize(30, 30)
fill_color_button.setStyleSheet("background-color: white")
fill_color_button.mousePressEvent = self.get_fill_color
border_color_button = QLabel()
border_color_button.setFixedSize(30, 30)
border_color_button.setStyleSheet("background-color: black")
border_color_button.mousePressEvent = self.get_border_color
border_width_label = QLabel("Border Width:")
self.border_width_combo_box = QComboBox()
self.border_width_combo_box.addItems(["1", "2", "3"])
self.border_width_combo_box.currentIndexChanged.connect(self.border_width_changed)
pen_style_label = QLabel("Pen Style:")
self.pen_style_combo_box = QComboBox()
self.pen_style_combo_box.addItems(["Solid", "Dash", "Dot"])
self.pen_style_combo_box.currentIndexChanged.connect(self.pen_style_changed)
layout = self.draw_widget.layout()
layout.addWidget(shape_label, 1, 0)
layout.addWidget(self.shape_combo_box, 1, 1)
layout.addWidget(fill_check_box, 1, 2)
layout.addWidget(fill_color_button, 1, 3)
layout.addWidget(border_color_button, 1, 4)
layout.addWidget(border_width_label, 2, 0)
layout.addWidget(self.border_width_combo_box, 2, 1)
layout.addWidget(pen_style_label, 2, 2)
layout.addWidget(self.pen_style_combo_box, 2, 3)
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Simple Drawing Dialog')
self.show()
def shape_changed(self, index):
self.draw_widget.shape = self.draw_widget.shapes[index]
self.draw_widget.update()
def fill_changed(self, state):
self.draw_widget.fill = state == Qt.Checked
self.draw_widget.update()
def get_fill_color(self, event):
color = QColorDialog.getColor()
if color.isValid():
self.draw_widget.fill_color = color
event.widget.setStyleSheet("background-color: {}".format(color.name()))
self.draw_widget.update()
def get_border_color(self, event):
color = QColorDialog.getColor()
if color.isValid():
self.draw_widget.border_color = color
event.widget.setStyleSheet("background-color: {}".format(color.name()))
self.draw_widget.update()
def border_width_changed(self, index):
self.draw_widget.border_width = index + 1
self.draw_widget.update()
def pen_style_changed(self, index):
if index == 0:
self.draw_widget.pen_style = Qt.SolidLine
elif index == 1:
self.draw_widget.pen_style = Qt.DashLine
elif index == 2:
self.draw_widget.pen_style = Qt.DotLine
self.draw_widget.update()
if __name__ == '__main__':
app = QApplication([])
main_widget = MainWidget()
app.exec_()
```
在这个代码示例中,我们创建了一个名为DrawWidget的自定义QWidget控件,用于实现绘图区域和鼠标事件处理。在MainWidget中,我们添加了一些控件,例如QComboBox、QCheckBox和QColorDialog,用于选择不同的形状、填充和边线属性。我们还实现了一些槽函数,以处理这些控件的状态变化事件。最后,我们使用QGridLayout将所有控件添加到DrawWidget中。
当用户在绘图区域中绘制形状时,DrawWidget会自动调用paintEvent函数进行绘制。在这个例子中,我们使用QPainter类提供的draw函数来绘制所选形状,并根据用户选择的填充和边线属性进行相应的填充和边线设置。
这个例子只是一个简单的示例,您可以根据您的需求对其进行更改和扩展。如果您需要更多帮助,请告诉我。
阅读全文