本文整理汇总了Python中PyQt5.QtWidgets.QGraphicsView.scene方法的典型用法代码示例。如果您正苦于以下问题:Python QGraphicsView.scene方法的具体用法?Python QGraphicsView.scene怎么用?Python QGraphicsView.scene使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QGraphicsView
的用法示例。
在下文中一共展示了QGraphicsView.scene方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BoardGUI
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import scene [as 别名]
class BoardGUI(QWidget):
"""cointain the graphical representation of the Board"""
# ratio of bordersize compared to the size of one base square
borderRatio = 0.8
baseRectRatio = 14/15 # 12/13 for normal ratio but looks weird
stoneScale = 0.46
# siganl
stoneClicked = pyqtSignal(tuple)
def __init__(self, parent, game):
super().__init__()
self.initUI(game)
def initUI(self, game):
self.board = game.currentBoard
self.game = game
self.showCoords = True
self.scene = QGraphicsScene()
# grid containing coordinates for the scene
self.grid = []
self.drawGrid()
# initialize and set layout + view
self.view = QGraphicsView(self.scene)
self.view.setMouseTracking(True)
self.view.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setMouseTracking(True)
box = QHBoxLayout()
box.addWidget(self.view)
self.setLayout(box)
# stones for all positions are created and listed in self.pos dict
self.createPosition()
self.makeCoords() # has to be called after drawGrid!
def resizeEvent(self, e):
self.view.fitInView(self.view.scene().sceneRect(), Qt.KeepAspectRatio)
def boardWidth(self):
"""returns the max width fitting into widget"""
width = self.contentsRect().width()*0.95
height = self.contentsRect().height()*0.95
return min(width, height*self.baseRectRatio)
def boardHeight(self):
"""returns the max width fitting into widget """
return self.boardWidth()*(1/self.baseRectRatio)
def makeGrid(self):
"""
returns coords [[(x, y)]] for the Grid according
to current window mesures
"""
# set scenesize to window size
self.scene.setSceneRect(0, 0, self.boardWidth(), self.boardHeight())
denom = self.board.size + 2*self.borderRatio
baseWidth = self.boardWidth() / denom
baseHeight = self.boardHeight() / denom
leftOffset = 0.5*baseWidth # (self.contentsRect().width()-self.boardWidth())/2
topOffset = 0 # (self.contentsRect().height()-self.boardHeight())/2
partionWidth = [leftOffset+(self.borderRatio+x)*baseWidth
for x in range(self.board.size)]
partionHeight = [topOffset+(self.borderRatio+x)*baseHeight
for x in range(self.board.size)]
grid = [[(x, y) for x in partionWidth] for y in partionHeight]
self.grid = grid
self.baseWidth = baseWidth
def drawGrid(self):
"""draws the background grid"""
self.makeGrid()
for line in self.grid:
self.scene.addLine(*line[0], *line[-1])
for (pointT, pointB) in zip(self.grid[0], self.grid[-1]):
self.scene.addLine(*pointT, *pointB)
self.drawHoshis()
def makeCoords(self):
""" draws Coordinates """
xLabels = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
yLabels = list(range(1, 26))
botGrid = []
leftGrid = []
# generate pixel coordinates grids
for n in range(self.board.size):
(xBot, yBot) = self.grid[self.board.size-1][n]
yBot += self.baseWidth*0.4/self.baseRectRatio
xBot -= self.baseWidth*0.1
botGrid.append((xBot, yBot))
(xLeft, yLeft) = self.grid[n][0]
xLeft -= self.baseWidth*1.2
#.........这里部分代码省略.........
示例2: View
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import scene [as 别名]
class View(QWidget):
trigger_focus_event = QtCore.pyqtSignal(str)
def __init__(self, emacs_xid, buffer, view_info):
super(View, self).__init__()
self.buffer = buffer
self.emacs_xid = emacs_xid
# Init widget attributes.
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_X11DoNotAcceptFocus, True)
self.setContentsMargins(0, 0, 0, 0)
self.installEventFilter(self)
# Init attributes.
self.view_info = view_info
(self.buffer_id, self.x, self.y, self.width, self.height) = view_info.split(":")
self.x = int(self.x)
self.y = int(self.y)
self.width = int(self.width)
self.height = int(self.height)
# Build QGraphicsView.
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.graphics_view = QGraphicsView(buffer, self)
self.graphics_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphics_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphics_view.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform | QPainter.TextAntialiasing)
# Remove damn border from QGraphicsView.
self.graphics_view.setFrameStyle(0)
self.graphics_view.setStyleSheet("QGraphicsView {background: transparent; border: 3px; outline: none;}")
self.layout.addWidget(self.graphics_view)
# NOTE: show function must start before resize to trigger *first* resizeEvent after show.
self.show()
# Resize after show to trigger fit view operation.
self.resize(self.width, self.height)
def resizeEvent(self, event):
# Fit content to view rect just when buffer fit_to_view option is enable.
if self.buffer.fit_to_view:
if event.oldSize().isValid():
self.graphics_view.fitInView(self.graphics_view.scene().sceneRect(), Qt.KeepAspectRatio)
QWidget.resizeEvent(self, event)
def eventFilter(self, obj, event):
# Focus emacs buffer when user click view.
if event.type() in [QEvent.MouseButtonPress, QEvent.MouseButtonRelease,
QEvent.MouseMove, QEvent.MouseButtonDblClick, QEvent.Wheel]:
# Send mouse event to applicatin view.
self.trigger_focus_event.emit("{0},{1}".format(event.globalX(), event.globalY()))
# Stop mouse event.
return True
return False
def showEvent(self, event):
# NOTE: we must reparent after widget show, otherwise reparent operation maybe failed.
self.reparent()
# Make graphics view at left-top corner after show.
self.graphics_view.verticalScrollBar().setValue(0)
self.graphics_view.horizontalScrollBar().setValue(0)
def reparent(self):
xlib_display = get_xlib_display()
view_xid = self.winId().__int__()
view_xwindow = xlib_display.create_resource_object("window", view_xid)
emacs_xwindow = xlib_display.create_resource_object("window", self.emacs_xid)
view_xwindow.reparent(emacs_xwindow, self.x, self.y)
xlib_display.sync()
def handle_destroy(self):
self.destroy()