如何从 QMainWindow 绘制 QGraphicsView
How to draw on QGraphicsView from QMainWindow
下面显示的 QgraphicsView 和 QMainWindow 的当前设计 class 是我在不同软件中的设计示例。
我必须向 QGraphicsView 添加滚动条。
原版软件所有的鼠标事件都在QMainWindow中处理。
问题:通过QMainWindow在QGraphicsView上绘图的方法是什么?
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
print "sdsads"
def mousePressEvent(self, ev):
item = QtGui.QGraphicsTextItem("")
item.setPos(ev.x(), ev.y())
self.scene.addItem(item)
print "ev.x() ", ev.x()
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
在QGraphicsView
中,向场景中添加项目是正常的,例如,如果你想绘制一个多边形,你必须使用QGraphicsPolygonItem
,如果你想获得正确的点,你必须使用 QGraphicsScene
而不是 QGraphicsView
.
在下面的示例中,您可以通过单击鼠标左键指定多边形点,然后单击鼠标右键完成绘制。
import sys
from PyQt4 import QtCore, QtGui
class GraphicsScene(QtGui.QGraphicsScene):
def __init__(self, *args, **kwargs):
QtGui.QGraphicsScene.__init__(self, *args, **kwargs)
self.polygon = None
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
self.polygon << ev.scenePos()
item = QtGui.QGraphicsPolygonItem(self.polygon)
item.setPen(QtGui.QPen(QtCore.Qt.red))
item.setBrush(QtGui.QBrush(QtCore.Qt.red))
self.addItem(item)
# or
# self.addPolygon(self.polygon, QtGui.QPen(QtCore.Qt.red), QtGui.QBrush(QtCore.Qt.red))
self.polygon = None
else:
if self.polygon is None:
self.polygon = QtGui.QPolygonF()
self.polygon << ev.scenePos()
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene =GraphicsScene(QtCore.QRectF(0, 0, 640, 480), self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
输出:
你有一个 XY problem,你正在寻找主要问题的解决方案,但不知道它是正确的,根据你的评论,你的主要问题是添加 QScrollBar
到 QMainWindow
,在那个元素中你想画图,所以没有必要使用 QGraphicsView
而是 QScrollArea
.
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.polygon = None
self.setFixedSize(640, 480)
self.pixmap = None
def mousePressEvent(self, ev):
if self.polygon is None:
self.polygon = QtGui.QPolygon()
self.polygon << ev.pos()
self.update()
def paintEvent(self, ev):
painter = QtGui.QPainter(self)
painter.fillRect(self.rect(), QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
painter.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap("exit.png"))
if self.polygon is not None:
painter.setPen(QtCore.Qt.blue)
painter.drawPolyline(self.polygon)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
scroll = QtGui.QScrollArea()
scroll.setBackgroundRole(QtGui.QPalette.Dark)
scroll.setWidget(Window())
scroll.setWidgetResizable(True)
self.setCentralWidget(scroll)
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
下面显示的 QgraphicsView 和 QMainWindow 的当前设计 class 是我在不同软件中的设计示例。
我必须向 QGraphicsView 添加滚动条。 原版软件所有的鼠标事件都在QMainWindow中处理。
问题:通过QMainWindow在QGraphicsView上绘图的方法是什么?
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
print "sdsads"
def mousePressEvent(self, ev):
item = QtGui.QGraphicsTextItem("")
item.setPos(ev.x(), ev.y())
self.scene.addItem(item)
print "ev.x() ", ev.x()
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
在QGraphicsView
中,向场景中添加项目是正常的,例如,如果你想绘制一个多边形,你必须使用QGraphicsPolygonItem
,如果你想获得正确的点,你必须使用 QGraphicsScene
而不是 QGraphicsView
.
在下面的示例中,您可以通过单击鼠标左键指定多边形点,然后单击鼠标右键完成绘制。
import sys
from PyQt4 import QtCore, QtGui
class GraphicsScene(QtGui.QGraphicsScene):
def __init__(self, *args, **kwargs):
QtGui.QGraphicsScene.__init__(self, *args, **kwargs)
self.polygon = None
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
self.polygon << ev.scenePos()
item = QtGui.QGraphicsPolygonItem(self.polygon)
item.setPen(QtGui.QPen(QtCore.Qt.red))
item.setBrush(QtGui.QBrush(QtCore.Qt.red))
self.addItem(item)
# or
# self.addPolygon(self.polygon, QtGui.QPen(QtCore.Qt.red), QtGui.QBrush(QtCore.Qt.red))
self.polygon = None
else:
if self.polygon is None:
self.polygon = QtGui.QPolygonF()
self.polygon << ev.scenePos()
class Window(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene =GraphicsScene(QtCore.QRectF(0, 0, 640, 480), self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
#self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
#self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
centralwidget = Window()
self.setCentralWidget(centralwidget)
centralwidget.scene.addPixmap(QtGui.QPixmap("exit.png"))
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())
输出:
你有一个 XY problem,你正在寻找主要问题的解决方案,但不知道它是正确的,根据你的评论,你的主要问题是添加 QScrollBar
到 QMainWindow
,在那个元素中你想画图,所以没有必要使用 QGraphicsView
而是 QScrollArea
.
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.polygon = None
self.setFixedSize(640, 480)
self.pixmap = None
def mousePressEvent(self, ev):
if self.polygon is None:
self.polygon = QtGui.QPolygon()
self.polygon << ev.pos()
self.update()
def paintEvent(self, ev):
painter = QtGui.QPainter(self)
painter.fillRect(self.rect(), QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
painter.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap("exit.png"))
if self.polygon is not None:
painter.setPen(QtCore.Qt.blue)
painter.drawPolyline(self.polygon)
class CityscapesLabelTool(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
scroll = QtGui.QScrollArea()
scroll.setBackgroundRole(QtGui.QPalette.Dark)
scroll.setWidget(Window())
scroll.setWidgetResizable(True)
self.setCentralWidget(scroll)
app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())