人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

python編程使用PyQt制作預(yù)覽窗口游戲中的小地圖

發(fā)布日期:2021-12-17 22:28 | 文章來(lái)源:源碼之家

寫(xiě)作思路

1、簡(jiǎn)述實(shí)現(xiàn)原理
2、部分代碼解析
3、位置同步解析(①上下兩屏位置同步②編輯屏位置保持不變)
效果圖如下:
版本1:

這就是我們常見(jiàn)的預(yù)覽窗口,上面預(yù)覽窗口移動(dòng)/縮放小方框都會(huì)導(dǎo)致下面的編輯界面位置發(fā)生變化,同理,下面的編輯界面的移動(dòng)/縮放也會(huì)導(dǎo)致上面的小方框變化,并且上面預(yù)覽圖是編輯窗口的同比例縮放

版本2:

在版本1的基礎(chǔ)上,加入了點(diǎn)的刪除和增加,并對(duì)畫(huà)布進(jìn)行了擴(kuò)展,同時(shí)保持編輯界面的畫(huà)面位置不變

1、簡(jiǎn)述實(shí)現(xiàn)原理

首先最重要的,要知道我們這些是用QGraphicsView、QGraphicsScene、QGraphicsRectItem 這三個(gè)基類(lèi)實(shí)現(xiàn)的
實(shí)現(xiàn)方法如下:
①Q(mào)GraphicsScene.render渲染編輯窗口獲得image,將image按照預(yù)覽窗口的比例進(jìn)行縮放并放入overView
②創(chuàng)建一個(gè)矩形框,框是按照編輯器窗口和image的比例進(jìn)行繪制的
③拖動(dòng)或者縮放預(yù)覽窗口的時(shí)候,編輯窗口按照同樣的比例移動(dòng)縮放,拖動(dòng)或者縮放預(yù)覽窗口的時(shí)候同理

2、部分代碼解析

①方框的完整代碼

from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QBrush, QPainterPath, QPainter, QColor, QPen
from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsItem
class GraphicsRectItem(QGraphicsRectItem):
 def __init__(self, scene, *args):
  super().__init__(*args)
  self.scene = scene
  self.setFlag(QGraphicsItem.ItemIsMovable, True)
  self.setFlag(QGraphicsItem.ItemIsSelectable, True)
  self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
  self.setFlag(QGraphicsItem.ItemIsFocusable, True)
 def shape(self):
  path = QPainterPath()
  path.addRect(self.rect())
  return path
 def paint(self, painter, option, widget=None):
  # 框選出來(lái)的方形
  painter.setBrush(QBrush(QColor(0, 0, 0, 0)))
  painter.setPen(QPen(QColor(0, 0, 0), 0.1, Qt.SolidLine))
  painter.drawRect(self.rect())
  x = self.rect().x()
  y = self.rect().y()
  width = self.rect().width()
  height = self.rect().height()
  otherColor = QColor(150, 150, 150, 50)
  painter.setBrush(QBrush(otherColor))
  # 下面這部分代碼是使得小方框以外的地方多一層蒙皮
  painter.setPen(QPen(QColor(0, 0, 0, 0), 1.0, Qt.SolidLine))
  painter.drawRect(QRectF(x-10000, y, 20000+width, -20000+height)) #上
  painter.drawRect(QRectF(x-10000, y+height, 20000+width, 20000+height)) #下
  painter.drawRect(QRectF(x, y, -20000, height)) #左
  painter.drawRect(QRectF(x+width, y, 20000, height)) #右
  painter.setRenderHint(QPainter.Antialiasing)
  painter.setBrush(QBrush(QColor(255, 0, 0, 255)))
  painter.setPen(QPen(QColor(0, 0, 0, 255), 1.0, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))

這就是前面提到的 “item” 《scene view item的使用》,QGraphicsRectItem 也是繼承自QGraphicsItem的,這里的方框就是我們要加到OverView界面中的那個(gè)定位方框

②編輯界面

class GraphicsWindow(QGraphicsView):
	def __init__(self, parent=None):
  super(GraphicsWindow, self).__init__(parent)
  ......
  self.scene = ViewPortGraphScene(self)
  self.setScene(self.scene)
  ......
  self.setSceneRect(-1 << 30, -1 << 30, 1 << 31, 1 << 31)
  self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
	def addPoint(self, x, y):
  self.scene.addEllipse(x, y, 16, 16, QPen(QColor(Qt.red)), QBrush(QColor(Qt.red)))
	def mousePressEvent(self, mouseEvent):
		......
  if mouseEvent.button() == Qt.LeftButton:
if isinstance(self.itemAt(mouseEvent.pos()), QGraphicsEllipseItem):
 self.scene.removeItem(self.itemAt(mouseEvent.pos()))
 self.parent.changeView()
  ......
  super(GraphicsWindow, self).mousePressEvent(mouseEvent)
class ViewPortGraphScene(QGraphicsScene):
 def __init__(self, parent=None):
 	super(ViewPortGraphScene, self).__init__(parent)
 	......
	def drawBackground(self, painter, rect):
		# 自己去畫(huà)格子吧 hhh

熟悉的操作:
1、創(chuàng)建scene
2、把scene放到view
3、把item放到scene,其中這里的item是點(diǎn)也就是QGraphicsEllipseItem,也是繼承自QGraphicsRectItem
使屏幕可以拖動(dòng): self.setSceneRect(-1 << 30, -1 << 30, 1 << 31, 1 << 31),因?yàn)閟cene很大,在view里面裝不下,所以就可以拖動(dòng)啦~
**添加點(diǎn):**如上的addPoint方法
**刪除點(diǎn):**如上的mousePressEvent方法,其中self.itemAt(mouseEvent.pos())可以獲取當(dāng)前鼠標(biāo)位置是什么東西

③預(yù)覽窗口

class OverViewGraphicsWindow(QGraphicsView):
 def __init__(self, parent=None):
  super(OverViewGraphicsWindow, self).__init__(parent)
  ......
  self.scene = OverViewGraphScene(self)
  self.item = GraphicsRectItem(self.scene, 0, 0, 50, 25)
  self.scene.addItem(self.item)
  ......
......
class OverViewGraphScene(QGraphicsScene):
 def __init__(self, parent=None):
  super(OverViewGraphScene, self).__init__(parent)

同樣的套路:
1、創(chuàng)建scene
2、把scene放到view
3、把item放到scene,其中這里的item是點(diǎn)也就是QGraphicsRectItem,繼承自QGraphicsRectItem

3、位置同步解析

①上下兩屏位置同步、編輯屏位置保持不變

1、兩個(gè)pyqtSignal 分別去響應(yīng)上下兩個(gè)屏幕的移動(dòng)和縮放
2、scene的的左上角到當(dāng)前屏幕中心的長(zhǎng)寬來(lái)定位(主要用到scene.itemsBoundingRect()、view.mapToScene()、view.mapFromScene()這幾個(gè)方法),屏幕中心的scene位置可以通過(guò)編輯窗口長(zhǎng)寬的一半并通過(guò)view.mapToScene()來(lái)轉(zhuǎn)化

以上就是python使用PyQt制作預(yù)覽窗口游戲中的小地圖的詳細(xì)內(nèi)容,更多關(guān)于PyQt制作預(yù)覽游戲小地圖窗口的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線(xiàn)路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線(xiàn)

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線(xiàn)
客服

在線(xiàn)客服:7*24小時(shí)在線(xiàn)

客服
熱線(xiàn)

400-630-3752
7*24小時(shí)客服服務(wù)熱線(xiàn)

關(guān)注
微信

關(guān)注官方微信
頂部