#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2016/11/28 04:40:38 +0900> u""" QCursorのテスト。マウスカーソルを変更する. 動作確認環境 : Windows10 x64 + Python 2.7.12 + PySide 1.2.4 """ import sys from PySide.QtCore import * # NOQA from PySide.QtGui import * # NOQA class GView(QGraphicsView): """GraphicsView.""" def __init__(self, *argv, **keywords): """init.""" super(GView, self).__init__(*argv, **keywords) scene = QGraphicsScene(self) self.setScene(scene) pm = QPixmap(320, 240) pm.fill(QColor(192, 192, 192, 255)) pitem = QGraphicsPixmapItem(pm) scene.addItem(pitem) lst = [ ("pen", "./mouse_cursor_pen.png", 0, 0), ("eraser", "./mouse_cursor_eraser.png", 0, 0), ("picker", "./mouse_cursor_picker.png", 9, 22), ("fill", "./mouse_cursor_fill.png", 9, 22), ] self.curs = [] for d in lst: name, filepath, hx, hy = d cur_pm = QPixmap(filepath) cur = QCursor(cur_pm, hotX=hx, hotY=hy) self.curs.append(cur) self.cur_idx = 0 self.setCursor(self.curs[self.cur_idx]) def mousePressEvent(self, event): u"""マウスボタンが押された時の処理.""" # 左もしくは右ボタンを押したらマウスカーソルを変更 if event.button() == Qt.LeftButton: self.cur_idx = (self.cur_idx + 1) % len(self.curs) elif event.button() == Qt.RightButton: self.cur_idx = (self.cur_idx - 1 + len(self.curs)) % len(self.curs) else: return self.setCursor(self.curs[self.cur_idx]) # マウスカーソルを設定 def mouseReleaseEvent(self, event): u"""マウスボタンが離された時の処理.""" pass # self.unsetCursor() class MyWidget(QWidget): """Main Window.""" def __init__(self, *argv, **keywords): """init.""" super(MyWidget, self).__init__(*argv, **keywords) l = QVBoxLayout() l.addWidget(QLabel("Dummy", self)) l.addWidget(QLabel("Dummy", self)) l.addWidget(GView(self)) l.addWidget(QLabel("Dummy", self)) l.addWidget(QLabel("Dummy", self)) self.setLayout(l) if __name__ == '__main__': app = QApplication(sys.argv) w = MyWidget() w.show() sys.exit(app.exec_())