如何从QMainWindow

时间:2018-04-06 06:42:38

标签: python qt pyqt qgraphicsview qmainwindow

下面显示的QgraphicsView和QMainWindow类的当前设计是我在不同软件中设计的一个示例。

我不得不在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_())

1 个答案:

答案 0 :(得分:1)

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_())

输出:

enter image description here

你有一个XY problem,你正在寻找解决主要问题的解决方案而不知道它是正确的,根据你评论的主要问题是添加{{1} } QScrollBar,并且在该元素中您想要制作图纸,因此没有必要使用QMainWindow而是QGraphicsView

QScrollArea

enter image description here