如何将图像放入 QGraphicsView 的滚动条区域? [英] How to put an image in QGraphicsView's scrollbar area?

查看:84
本文介绍了如何将图像放入 QGraphicsView 的滚动条区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些限制,图像必须在 QMainWindow 类中,滚动条必须在 QGraphicsView 类中.

Due to some restrictions image has to be in the class QMainWindow and scrollbars have to be the QGraphicsView class.

这意味着我必须通过 QMainWindow 类在 QGraphicsView 类中添加图像.exit.png"存在于我运行此代码的文件夹中.

This means that I have to add image in QGraphicsView class through QMainWindow class. "exit.png" exists in the folder from where I run this code.

添加这张图片的正确方法是什么?

What is the proper way to add this picture?

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"


class CityscapesLabelTool(QtGui.QMainWindow):
    def __init__(self, parent=None):

        QtGui.QMainWindow.__init__(self, parent)
        centralwidget = Window()
        self.setCentralWidget(centralwidget) 

        centralwidget.scene.image = QtGui.QImage("exit.png")


app = QtGui.QApplication(sys.argv)
GUI = CityscapesLabelTool()
GUI.show()
sys.exit(app.exec_())

推荐答案

对于这种情况,解决方案是使用 QGraphicsPixmapItem:

For this case the solution is to use a QGraphicsPixmapItem:

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"))
        # or
        # item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap("exit.png"))
        # centralwidget.scene.addItem(item) 

这篇关于如何将图像放入 QGraphicsView 的滚动条区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆