当前位置:网站首页 > R语言数据分析 > 正文

qpainterpath 平移(qpainter drawpie)



点击上方“机械电子工程技术”关注我们





在两个GraphicsView控件中,一个画圆形,一个画方形,通过两个按钮控制
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QHBoxLayout, QVBoxLayout, QWidget, QPushButtonfrom PyQt5.QtGui import QPainter, QPen, QColor

class GraphicsWidget(QWidget): def __init__(self): super().__init__()
# 创建场景和视图1 self.scene1 = QGraphicsScene() self.view1 = QGraphicsView(self.scene1) self.circle_item1 = None
# 创建场景和视图2 self.scene2 = QGraphicsScene() self.view2 = QGraphicsView(self.scene2) self.rect_item1 = None
# 添加按钮 self.btn1 = QPushButton('画圆形') self.btn2 = QPushButton('画方形')
# 将按钮添加到垂直布局中 vlayout = QVBoxLayout() vlayout.addWidget(self.btn1) vlayout.addWidget(self.btn2)
# 使用水平布局将两个视图和按钮包装在一起 layout = QHBoxLayout() layout.addWidget(self.view1) layout.addWidget(self.view2) layout.addLayout(vlayout)
self.setLayout(layout)
self.btn1.clicked.connect(self.draw_circle) self.btn2.clicked.connect(self.draw_rect)
def draw_circle(self): if self.circle_item1 is not None: return
# 在场景1中添加红色圆形 self.circle_item1 = self.scene1.addEllipse(0, 0, 50, 50, QPen(QColor("red"))) self.view1.setScene(self.scene1)
def draw_rect(self): if self.rect_item1 is not None: return
# 在场景2中添加蓝色矩形 self.rect_item1 = self.scene2.addRect(0, 0, 50, 50, QPen(QColor("blue"))) self.view2.setScene(self.scene2)

if __name__ == '__main__': app = QApplication([]) widget = GraphicsWidget() widget.show() app.exec_()
两个GraphicsView控件中分别显示打开的图片,点击按钮后弹出文件对话框选择图片,显示在GraphicsView控件中,是两个按钮,一个对应一个GraphicsView控件,图片显示要匹配控件大小
from PyQt5.QtGui import QPixmapfrom PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QFileDialog, QGraphicsScene, QGraphicsPixmapItem, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QMessageBoximport sys
class MainWindow(QMainWindow): def __init__(self): super().__init__()
# 初始化UI元素 self.setWindowTitle("GraphicsView显示图片") self.resize(800, 600)
# 添加两个GraphicsView控件 self.graphicsView1 = QGraphicsView() self.graphicsView2 = QGraphicsView()
# 添加两个PushButton按钮 btn1 = QPushButton("选择图片1", self) btn2 = QPushButton("选择图片2", self)
# 连接按钮的clicked信号到槽函数 btn1.clicked.connect(self.load_image_1) btn2.clicked.connect(self.load_image_2)
# 创建两个竖直布局器,并添加相应的控件 vbox1 = QVBoxLayout() vbox1.addWidget(self.graphicsView1) vbox1.addWidget(btn1)
vbox2 = QVBoxLayout() vbox2.addWidget(self.graphicsView2) vbox2.addWidget(btn2)
# 创建一个水平布局器 hbox = QHBoxLayout() hbox.addLayout(vbox1) hbox.addLayout(vbox2)
# 创建一个QWidget并将水平布局器设置为它的布局 widget = QWidget() widget.setLayout(hbox)
# 将QWidget设置为CentralWidget,使其成为主窗口的内容 self.setCentralWidget(widget)
def load_image_1(self): # 弹出文件对话框选择图片 file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "*.png;;*.jpg;;*.bmp") if file_name: # 加载图像 pixmap = QPixmap(file_name)
# 调整图像大小以匹配GraphicsView控件 scene = QGraphicsScene() item = QGraphicsPixmapItem(pixmap.scaled(self.graphicsView1.size())) scene.addItem(item)
# 设置GraphicsView的Scene self.graphicsView1.setScene(scene)
def load_image_2(self): # 弹出文件对话框选择图片 file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "*.png;;*.jpg;;*.bmp") if file_name: # 加载图像 pixmap = QPixmap(file_name)
# 调整图像大小以匹配GraphicsView控件 scene = QGraphicsScene() item = QGraphicsPixmapItem(pixmap.scaled(self.graphicsView2.size())) scene.addItem(item)
# 设置GraphicsView的Scene self.graphicsView2.setScene(scene)

if __name__ == "__main__":

app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())

想了解更多

赶紧扫码关注

到此这篇qpainterpath 平移(qpainter drawpie)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • detr源码(dev源码)2025-04-08 20:09:07
  • srored是什么意思(shorer是什么意思)2025-04-08 20:09:07
  • 反编译exe文件成源码(reflector反编译exe)2025-04-08 20:09:07
  • PDFviewer官方下载(pdf viewer下载)2025-04-08 20:09:07
  • ueditor编辑器采集公众号(ueditor获取编辑器内容)2025-04-08 20:09:07
  • entware命令(命令enable)2025-04-08 20:09:07
  • swagger无法访问(swagger-ui访问不到)2025-04-08 20:09:07
  • theater(theater英文翻译)2025-04-08 20:09:07
  • 连接redis(连接redis用什么包)2025-04-08 20:09:07
  • druid监控页面默认账号密码(druid 监控页面)2025-04-08 20:09:07
  • 全屏图片