python hover_pyside.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python hover_pyside.py相关的知识,希望对你有一定的参考价值。

#!python2
# -*- coding: utf-8 -*-

import sys
from PySide import QtCore, QtGui


class testWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):

        super(testWindow, self).__init__(parent)

        self.scene = Scene(QtCore.QRect(0, 0, 640, 480))
        self.scene.addRect(0, 0, 640, 480, QtGui.QPen(QtCore.Qt.black), QtGui.QBrush(QtCore.Qt.white))
        self.view  = QtGui.QGraphicsView(self.scene)
        self.view.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.gray))

        # itemを配置
        item = TestItem(QtCore.QRect(0, 0, 100, 100))
        self.scene.addItem(item)
        child = TestItem(QtCore.QRect(150, 0, 100, 100))
        self.scene.addItem(child)
        child.setParentItem(item)

        self.setCentralWidget(self.view)
        self.setGeometry(100, 100, 700, 500)


class Scene(QtGui.QGraphicsScene):

    mouse_pos = None
    sel_item  = None

    def __init__(self, *args, **kwargs):

        super(Scene, self).__init__(*args, **kwargs)


class TestItem(QtGui.QGraphicsItem):

    rect = None
    color = QtGui.QColor('pink')

    def __init__(self, rect, parent=None):
        super(TestItem, self).__init__(parent)

        self.__rect = rect
        self.setAcceptHoverEvents(True)

    def boundingRect(self):

        return self.__rect

    def paint(self, painter, option, widget):

        painter.setBrush(self.color)
        painter.drawRoundedRect(self.__rect, 5, 5)

    def hoverEnterEvent(self, e):

        if self.isUnderMouse():
            self.color = QtGui.QColor('yellow')
            self.update()

    def hoverLeaveEvent(self, e):

        self.color = QtGui.QColor('pink')
        self.update()

    def changeRect(self, rect):

        self.__rect = rect
        self.update()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    a = testWindow()
    a.show()
    sys.exit(app.exec_())

以上是关于python hover_pyside.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python