本文整理汇总了Python中PyQt5.QtWidgets.QGraphicsView.fitInView方法的典型用法代码示例。如果您正苦于以下问题:Python QGraphicsView.fitInView方法的具体用法?Python QGraphicsView.fitInView怎么用?Python QGraphicsView.fitInView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QGraphicsView
的用法示例。
在下文中一共展示了QGraphicsView.fitInView方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: actionPrintSlot
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [as 别名]
def actionPrintSlot(self) -> None:
printer = QPrinter()
printer.setPageOrientation(QPageLayout.Landscape)
if QPrintDialog(printer).exec_():
painter = QPainter(printer)
painter.setRenderHint(QPainter.Antialiasing)
view = QGraphicsView()
view.setScene(self.scene)
view.setSceneRect(QRectF(0, 0, 290, 200))
view.fitInView(QRectF(0, 0, 290, 200), Qt.KeepAspectRatio)
view.scale(1, -1)
view.render(painter)
del painter # necessary, thanks Qt
示例2: main
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [as 别名]
def main():
app = QApplication(sys.argv)
grview = QGraphicsView()
scene = QGraphicsScene()
scene.setSceneRect(0, 0, 680, 459)
scene.addPixmap(QPixmap('../images/phototest.tif'))
grview.setScene(scene)
item = AreaItem(0, 0, 300, 150)
item.areaColor = QColor(155, 155, 0, 75)
scene.addItem(item)
grview.fitInView(scene.sceneRect(), Qt.KeepAspectRatio)
grview.show()
sys.exit(app.exec_())
示例3: BoardGUI
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [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
#.........这里部分代码省略.........
示例4: View
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [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()
示例5: QGraphicsView
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [as 别名]
w = QtWidgets.QWidget()
v = QGraphicsView()
def quit():
print("Connection closed by server")
app.quit()
iface = NetworkInterface()
t = QtCore.QThread()
iface.moveToThread(t)
t.start()
QtCore.QMetaObject.invokeMethod(iface, "run", Qt.QueuedConnection)
scene = QGraphicsScene(v)
v.setRenderHints(QtGui.QPainter.HighQualityAntialiasing)
v.setScene(scene)
size = blobs.BOARD_SIZE
v.fitInView(0, 0, size, size)
field = GameField(size, scene)
iface.dataReceived.connect(field.update, Qt.QueuedConnection)
iface.connectionClosed.connect(quit)
w.setLayout(QtWidgets.QHBoxLayout())
w.resize(800, 600)
w.layout().addWidget(v)
w.setWindowFlags(Qt.Dialog)
w.show()
app.exec_()
示例6: Spectrum
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsView [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsView import fitInView [as 别名]
#.........这里部分代码省略.........
data : ndarray
1D vector containing the data only
This function can be called by self.display_window (which reads the
data for the selected channel) or by the mouse-events functions in
traces (which read chunks of data from the user-made selection).
"""
value = self.config.value
self.scene = QGraphicsScene(value['x_min'], value['y_min'],
value['x_max'] - value['x_min'],
value['y_max'] - value['y_min'])
self.idx_fig.setScene(self.scene)
self.add_grid()
self.resizeEvent(None)
s_freq = self.parent.traces.data.s_freq
f, Pxx = welch(data, fs=s_freq,
nperseg=int(min((s_freq, len(data))))) # force int
freq_limit = (value['x_min'] <= f) & (f <= value['x_max'])
if self.config.value['log']:
Pxx_to_plot = log(Pxx[freq_limit])
else:
Pxx_to_plot = Pxx[freq_limit]
self.scene.addPath(Path(f[freq_limit], Pxx_to_plot),
QPen(QColor(LINE_COLOR), LINE_WIDTH))
def add_grid(self):
"""Add axis and ticks to figure.
Notes
-----
I know that visvis and pyqtgraphs can do this in much simpler way, but
those packages create too large a padding around the figure and this is
pretty fast.
"""
value = self.config.value
# X-AXIS
# x-bottom
self.scene.addLine(value['x_min'], value['y_min'],
value['x_min'], value['y_max'],
QPen(QColor(LINE_COLOR), LINE_WIDTH))
# at y = 0, dashed
self.scene.addLine(value['x_min'], 0,
value['x_max'], 0,
QPen(QColor(LINE_COLOR), LINE_WIDTH, Qt.DashLine))
# ticks on y-axis
y_high = int(floor(value['y_max']))
y_low = int(ceil(value['y_min']))
x_length = (value['x_max'] - value['x_min']) / value['x_tick']
for y in range(y_low, y_high):
self.scene.addLine(value['x_min'], y,
value['x_min'] + x_length, y,
QPen(QColor(LINE_COLOR), LINE_WIDTH))
# Y-AXIS
# left axis
self.scene.addLine(value['x_min'], value['y_min'],
value['x_max'], value['y_min'],
QPen(QColor(LINE_COLOR), LINE_WIDTH))
# larger ticks on x-axis every 10 Hz
x_high = int(floor(value['x_max']))
x_low = int(ceil(value['x_min']))
y_length = (value['y_max'] - value['y_min']) / value['y_tick']
for x in range(x_low, x_high, 10):
self.scene.addLine(x, value['y_min'],
x, value['y_min'] + y_length,
QPen(QColor(LINE_COLOR), LINE_WIDTH))
# smaller ticks on x-axis every 10 Hz
y_length = (value['y_max'] - value['y_min']) / value['y_tick'] / 2
for x in range(x_low, x_high, 5):
self.scene.addLine(x, value['y_min'],
x, value['y_min'] + y_length,
QPen(QColor(LINE_COLOR), LINE_WIDTH))
def resizeEvent(self, event):
"""Fit the whole scene in view.
Parameters
----------
event : instance of Qt.Event
not important
"""
value = self.config.value
self.idx_fig.fitInView(value['x_min'],
value['y_min'],
value['x_max'] - value['x_min'],
value['y_max'] - value['y_min'])
def reset(self):
"""Reset widget as new"""
self.idx_chan.clear()
if self.scene is not None:
self.scene.clear()
self.scene = None