python編程使用PyQt創(chuàng)建UE藍(lán)圖
實(shí)現(xiàn)思路
1、場地部署:我們需要擁有一個(gè)可以用來畫節(jié)點(diǎn)的地方!詳看我這篇文章QGraphicsScene、QGraphicsView的基礎(chǔ)使用,這篇文章用的也是同樣的方法PyQt制作預(yù)覽窗口(游戲中的小地圖)
2、節(jié)點(diǎn)創(chuàng)建:我們需要自定義節(jié)點(diǎn),也就是下圖中的方框內(nèi)的東西,主要涉及到的就是Qt中的QGraphicsItem,通過繼承這個(gè)類來自定義節(jié)點(diǎn)樣式
3、連線:涉及到的就是Qt中的QGraphicsLineItem,繼承這個(gè)類,并在paint中自定義連線樣式,比如我這里使用的是qt自帶的貝塞爾曲線
實(shí)現(xiàn)的效果如下,節(jié)點(diǎn)之間可以通過端口互相連接
1、場地部署
class EditorView(QGraphicsView): def __init__(self, parent=None): super(EditorView, self).__init__(parent) self.parent = parent self.scaleFactor = 1 self.lastPos = QPointF() self.scene = EditorScene(self) self.setScene(self.scene) self.setSceneRect(-1 << 30, -1 << 30, 1 << 31, 1 << 31) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.verticalScrollBar().setEnabled(False) self.horizontalScrollBar().setEnabled(False) class EditorScene(QGraphicsScene): def __init__(self, parent=None): super(EditorScene, self).__init__(parent) def drawBackground(self, painter, rect): pass # 在這里畫底圖,也就是上面的方格圖
2、節(jié)點(diǎn)創(chuàng)建
下面是創(chuàng)建節(jié)點(diǎn)的主體,就那個(gè)黑框框的東西
class NodeItem(QGraphicsItem): def __init__(self, parent=None): super(NodeItem, self).__init__(parent) self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.setFlag(QGraphicsItem.ItemIsFocusable, True) self.nodeName = "Node" self.width = 150 self.height = 50 self.addPort() def addPort(self): leftPort = PortItem(self) leftPort.setPos(5, self.height/2.7) rightPort = PortItem(self) rightPort.setPos(self.width-20, self.height/2.7) def paint(self, painter, style, *args, **kwargs): brush = QBrush(QColor(0xaa, 0xaa, 0xaa, 0xaa)) painter.setBrush(brush) pen = QPen() pen.setWidth(1) painter.setPen(pen) painter.drawRect(0, 0, self.width, self.height) painter.drawText(self.width/2.5, self.height/1.8, self.nodeName)
下面是節(jié)點(diǎn)端口的創(chuàng)建
class PortItem(QGraphicsItem): def __init__(self, parent=None): super(PortItem, self).__init__(parent) self.portDiam = 15 def paint(self, painter, style, *args, **kwargs): portColor = QColor(0x00, 0xaa, 0x00, 0x66) painter.setBrush(portColor) pen = QPen() pen.setColor(portColor) pen.setWidth(2) painter.setPen(pen) painter.drawEllipse(0, 0, self.portDiam, self.portDiam)
在節(jié)點(diǎn)NodeItem里面,創(chuàng)建兩個(gè)端口用于連接
3、連線
①首先是連線類的創(chuàng)建
class LineItem(QGraphicsItem): def __init__(self, posStart, posEnd, parent=None): super(LineItem, self).__init__(parent) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.setFlag(QGraphicsItem.ItemIsFocusable, True) self.posStart = posStart self.posEnd = posEnd def paint(self, painter, style, *args, **kwargs): midPos = (self.posStart + self.posEnd)/2 lineColor = QColor(0xff, 0x00, 0x00, 0xff) pen = QPen() pen.setColor(lineColor) pen.setWidth(2) painter.setPen(pen) linePath = QPainterPath() linePath.moveTo(self.posStart) linePath.cubicTo(QPointF(midPos.x(), self.posStart.y()), midPos, self.posEnd) painter.drawPath(linePath)
②如何連接節(jié)點(diǎn)
def mouseReleaseEvent(self, event): self.line = LineItem(self.portPosStart, self.portPosEnd) self.scene.addItem(self.line)
ps寫在最后,如果你的圖沒有刷新,你可以先把窗口縮小再打開,他就會刷新了,如果你想讓他自動刷新,就調(diào)用scene.update()方法吧!
以上就是python編程使用PyQt創(chuàng)建UE藍(lán)圖的詳細(xì)內(nèi)容,更多關(guān)于PyQt創(chuàng)建UE藍(lán)圖的資料請關(guān)注腳本之家其它相關(guān)文章!
- python編程PyQt5創(chuàng)建按鈕及觸發(fā)點(diǎn)擊事件示例解析
- python光學(xué)仿真PyQt5基礎(chǔ)框架教程
- python編程使用PyQt制作預(yù)覽窗口游戲中的小地圖
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5菜單和工具欄功能作用
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5安裝與環(huán)境配置過程詳解
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5基本控件使用解析
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5信號與槽的連接
- python PyQt實(shí)現(xiàn)的手寫電子簽名程序?qū)嵗骄?/a>
相關(guān)文章
Python django使用多進(jìn)程連接mysql錯(cuò)誤的解決方法
這篇文章主要介紹了Python django使用多進(jìn)程連接mysql錯(cuò)誤的解決方法,詳細(xì)的介紹了解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10詳解python數(shù)據(jù)結(jié)構(gòu)和算法
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)和算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04