栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python PyQt:如何用鼠标在窗口上移动小部件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python PyQt:如何用鼠标在窗口上移动小部件?

您应该查看QGraphicsView而不是正在执行的操作,它已经内置了所有功能。

http://doc.qt.nokia.com/4.7-snapshot/qgraphicsview.html

http://doc.qt.nokia.com/4.7-snapshot/qgraphicsscene.html

from PyQt4 import QtGui, QtCoreclass Myframe(QtGui.QGraphicsView):    def __init__( self, parent = None ):        super(Myframe, self).__init__(parent)        self.setScene(QtGui.QGraphicsScene())        # add some items        x = 0        y = 0        w = 45        h = 45        pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))        brush = QtGui.QBrush(pen.color().darker(150))        item = self.scene().addEllipse(x, y, w, h, pen, brush)        item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)if ( __name__ == '__main__' ):    app = QtGui.QApplication([])    f = Myframe()    f.show()    app.exec_()

编辑:显示如何创建QPainterPath

from PyQt4 import QtGui, QtCoreclass Myframe(QtGui.QGraphicsView):    def __init__( self, parent = None ):        super(Myframe, self).__init__(parent)        scene = QtGui.QGraphicsScene()        self.setScene(scene)        # add some items        x = 0        y = 0        w = 45        h = 45        pen   = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))        brush = QtGui.QBrush(pen.color().darker(150))        item = scene.addEllipse(x, y, w, h, pen, brush)        item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)        # create an open path        path = QtGui.QPainterPath()        path.moveTo(-w, -h)        path.lineTo(-w, h)        path.lineTo(w, h)        path.lineTo(w, -h)        clr   = QtGui.QColor('blue')        clr.setAlpha(120)        brush = QtGui.QBrush(clr)        pen   = QtGui.QPen(QtCore.Qt.NoPen)        fill_item = scene.addRect(-w, y, w*2, h, pen, brush)        path_item = scene.addPath(path)if ( __name__ == '__main__' ):    app = QtGui.QApplication([])    f = Myframe()    f.show()    app.exec_()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号