-
-
Save vabock/709297 to your computer and use it in GitHub Desktop.
PySide範囲選択テスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
from PySide import QtCore, QtGui | |
from PySide.QtCore import Qt | |
import selwinui | |
class SelRect(QtGui.QGraphicsRectItem): | |
def __init__(self, *args): | |
super(SelRect, self).__init__(*args) | |
def paint(self, painter, option, widget): | |
# painter.eraseRect(self.boundingRect()) | |
painter.setCompositionMode(QtGui.QPainter.RasterOp_SourceXorDestination) | |
pen = QtGui.QPen(Qt.white) | |
pen.setWidth(10) | |
painter.setBrush(Qt.NoBrush) | |
painter.setPen(pen) | |
painter.drawRect(self.boundingRect().adjusted(5, 5, -5, -5)) | |
class MyScene(QtGui.QGraphicsScene): | |
def __init__(self, *args): | |
super(MyScene, self).__init__(*args) | |
def mouseReleaseEvent(self, event): | |
if event.button() == Qt.LeftButton: | |
r = SelRect() | |
r.setRect(QtCore.QRect(event.buttonDownScenePos(Qt.LeftButton).toPoint(), | |
event.scenePos().toPoint()).normalized()) | |
self.addItem(r) | |
class MyWidget(QtGui.QWidget): | |
def __init__(self, *args): | |
super(MyWidget, self).__init__(*args) | |
self.image = None | |
self.setMouseTracking(True) | |
self._pos = None | |
def loadImage(self, name): | |
self.image = QtGui.QImage(name) | |
self.setFixedSize(self.image.size()) | |
def mouseMoveEvent(self, event): | |
p = event.pos() | |
if not self._pos or p != self._pos: | |
sp = self._pos | |
self._pos = p | |
if sp: | |
self.update(0, 0, max(p.x(), sp.x()), max(p.y(), sp.y())) | |
def paintEvent(self, event): | |
painter = QtGui.QPainter(self) | |
if self.image: | |
painter.drawImage(0, 0, self.image) | |
if self._pos: | |
# print self._pos | |
painter.setCompositionMode(QtGui.QPainter.RasterOp_SourceXorDestination) | |
pen = QtGui.QPen(Qt.white) | |
pen.setWidth(10) | |
painter.setPen(pen) | |
painter.setBrush(Qt.NoBrush) | |
# painter.setBrush(Qt.cyan) | |
rect = QtCore.QRect(0, 0, self._pos.x(), self._pos.y()) | |
rect.adjust(5, 5, -5, -5) | |
painter.drawRect(rect) | |
class MainWindow(QtGui.QMainWindow, selwinui.Ui_MainWindow): | |
def __init__(self): | |
super(MainWindow, self).__init__() | |
self.setupUi(self) | |
img = QtGui.QImage('selwin.png') | |
self.pixmap = QtGui.QPixmap.fromImage(img) | |
scene = MyScene(self) | |
scene.addPixmap(self.pixmap) | |
self.graphicsView.setScene(scene) | |
self.scene = scene | |
w = MyWidget(self.scrollArea) | |
w.loadImage('selwin.png') | |
self.scrollArea.setWidget(w) | |
self.actionOpen.triggered.connect(self.open) | |
def eventFilter(self, object, event): | |
print type(event), event.type() | |
# if event.type() == QtCore.QEvent.GraphicsSceneMouseRelease: | |
# p0 = event.buttonDownScenePos(Qt.LeftButton) | |
# p1 = event.scenePos() | |
# print p0, p1 | |
def open(self): | |
QtGui.QMessageBox.information(self, u'Post', u'送信しました') | |
if __name__ == "__main__": | |
import sys | |
app = QtGui.QApplication(sys.argv) | |
window = MainWindow() | |
window.show() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>583</width> | |
<height>497</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>MainWindow</string> | |
</property> | |
<widget class="QWidget" name="centralwidget"> | |
<layout class="QVBoxLayout" name="verticalLayout"> | |
<item> | |
<widget class="QScrollArea" name="scrollArea"> | |
<property name="widgetResizable"> | |
<bool>true</bool> | |
</property> | |
<widget class="QWidget" name="scrollAreaWidgetContents"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>561</width> | |
<height>204</height> | |
</rect> | |
</property> | |
</widget> | |
</widget> | |
</item> | |
<item> | |
<widget class="QGraphicsView" name="graphicsView"/> | |
</item> | |
</layout> | |
</widget> | |
<widget class="QMenuBar" name="menubar"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>583</width> | |
<height>18</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QStatusBar" name="statusbar"/> | |
<widget class="QToolBar" name="toolBar"> | |
<property name="windowTitle"> | |
<string>toolBar</string> | |
</property> | |
<attribute name="toolBarArea"> | |
<enum>TopToolBarArea</enum> | |
</attribute> | |
<attribute name="toolBarBreak"> | |
<bool>false</bool> | |
</attribute> | |
<addaction name="actionOpen"/> | |
</widget> | |
<action name="actionOpen"> | |
<property name="text"> | |
<string>開く</string> | |
</property> | |
</action> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment