pyqt之QGraphics系列----节点编辑器(二)
2018-10-19 本文已影响0人
WoHowLearn
仓库地址 https://gitlab.com/pavel.krupala/pyqt-node-editor-tutorials.git
image.png效果图
image.png- node_editor_wnd.py
增加了一些测试
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from node_graphics_scene import QDMGraphicsScene
from node_graphics_view import QDMGraphicsView
class NodeEditorWnd(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
...
self.addDebugContent()
def addDebugContent(self):
greenBrush = QBrush(Qt.green)
outlinePen = QPen(Qt.black)
outlinePen.setWidth(2)
rect = self.grScene.addRect(-100, -100, 80, 100, outlinePen, greenBrush)
rect.setFlag(QGraphicsItem.ItemIsMovable)
text = self.grScene.addText("This is my Awesome text!", QFont("Ubuntu"))
text.setFlag(QGraphicsItem.ItemIsSelectable)
text.setFlag(QGraphicsItem.ItemIsMovable)
text.setDefaultTextColor(QColor.fromRgbF(1.0, 1.0, 1.0))
widget1 = QPushButton("Hello World")
proxy1 = self.grScene.addWidget(widget1)
proxy1.setFlag(QGraphicsItem.ItemIsMovable)
proxy1.setPos(0, 30)
widget2 = QTextEdit()
proxy2 = self.grScene.addWidget(widget2)
proxy2.setFlag(QGraphicsItem.ItemIsSelectable)
proxy2.setPos(0, 60)
line = self.grScene.addLine(-200, -200, 400, -100, outlinePen)
line.setFlag(QGraphicsItem.ItemIsMovable)
line.setFlag(QGraphicsItem.ItemIsSelectable)
- node_graphics_view.py
from PyQt5.QtWidgets import QGraphicsView
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class QDMGraphicsView(QGraphicsView):
def __init__(self, grScene, parent=None):
super().__init__(parent)
self.grScene = grScene
self.initUI()
self.setScene(self.grScene)
def initUI(self):
# 图像品质
self.setRenderHints(QPainter.Antialiasing | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform)
# 全部刷新
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
selfRR.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
QGraphicsItem 的flag 一般设置为 setFlag(QGraphicsItem.ItemIsMovable)
和
setFlag(QGraphicsItem.ItemIsSelectable)
表示可移动和可选择