python编程使用PyQt创建UE蓝图
作者:在逆境中蜕变 时间:2023-11-20 14:24:58
实现思路
1、场地部署:我们需要拥有一个可以用来画节点的地方!详看我这篇文章QGraphicsScene、QGraphicsView的基础使用,这篇文章用的也是同样的方法PyQt制作预览窗口(游戏中的小地图)
2、节点创建:我们需要自定义节点,也就是下图中的方框内的东西,主要涉及到的就是Qt中的QGraphicsItem,通过继承这个类来自定义节点样式
3、连线:涉及到的就是Qt中的QGraphicsLineItem,继承这个类,并在paint中自定义连线样式,比如我这里使用的是qt自带的贝塞尔曲线
实现的效果如下,节点之间可以通过端口互相连接
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、节点创建
下面是创建节点的主体,就那个黑框框的东西
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)
下面是节点端口的创建
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)
在节点NodeItem里面,创建两个端口用于连接
3、连线
①首先是连线类的创建
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)
②如何连接节点
def mouseReleaseEvent(self, event):
self.line = LineItem(self.portPosStart, self.portPosEnd)
self.scene.addItem(self.line)
ps写在最后,如果你的图没有刷新,你可以先把窗口缩小再打开,他就会刷新了,如果你想让他自动刷新,就调用scene.update()方法吧!
来源:https://blog.csdn.net/weixin_40301728/article/details/111569708
标签:python,PyQt,UE,蓝图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
windows系统下让mysql支持federated的storage engine
2010-01-20 11:16:00
解决pycharm中导入自己写的.py函数出错问题
2023-07-09 12:12:05
![](https://img.aspxhome.com/file/2023/2/60302_0s.jpg)
为什么相对PHP黑python的更少
2024-05-21 10:20:51
ASP中3种分页显示的性能比较
2007-08-15 13:37:00
django 在原有表格添加或删除字段的实例
2023-11-25 04:21:08
多栏自适应布局问题浅谈
2010-08-16 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20108/16/two_column_layout-58s.png)
Python Threading 线程/互斥锁/死锁/GIL锁
2021-03-24 12:21:25
Golang中如何对MySQL进行操作详解
2024-01-28 23:16:53
ASP看代码学习CASE的用法
2008-04-13 06:36:00
CSS控制鼠标样式变换方法
2007-11-17 07:58:00
Vue中keep-alive组件的深入理解
2023-07-02 17:00:38
![](https://img.aspxhome.com/file/2023/4/139884_0s.gif)
python区分不同数据类型的方法
2022-03-30 23:37:41
![](https://img.aspxhome.com/file/2023/2/134562_0s.png)
完美解决python3.7 pip升级 拒绝访问问题
2023-08-10 07:45:33
![](https://img.aspxhome.com/file/2023/6/128626_0s.png)
M1芯片安装python3.9.1的实现
2021-12-15 12:13:46
![](https://img.aspxhome.com/file/2023/6/135036_0s.jpg)
Python下简易的单例模式详解
2022-02-15 10:03:19
php基础教程
2024-06-05 09:23:33
Go定时器cron的使用详解
2024-04-28 10:47:43
js原生map实现的方法总结
2024-04-10 10:47:34
基于Python实现简单的人脸识别系统
2021-04-07 17:58:19
![](https://img.aspxhome.com/file/2023/0/80810_0s.jpg)
asp如何实现页面执行时间及搜索时间
2007-11-12 22:48:00