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

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

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_()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5646194.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存