"PYQT4 Instance 11" various graphics Items

Source: Internet
Author: User
#-*-coding:utf-8-*-from pyqt4.qtcore Import * from Pyqt4.qtgui import * Import Math Qtextcodec.setcodecfortr (qtextcod Ec.codecforname ("Utf-8")) class Flashitem (Qgraphicsitem): def __init__ (Self,qobject): Super (Flashitem,self). _ _init__ () Self.timer = Qtimer () Self.flash = True Self.setflag (self.
        itemismovable) Aa=qobject () Aa.starttimer (M) def boundingrect (self): adjust = 2 Return QRECTF ( -10-adjust,-10-adjust,43 + adjust,43 + adjust) def paint (self,painter,option,widget): P Ainter.setpen (Qt.nopen) Painter.setbrush (Qt.darkgray) painter.drawellipse ( -7,-7,40,40) PA Inter.setpen (Qpen (qt.black,0)) Painter.setbrush (Self.flash if qt.red else Qt.yellow) painter.drawellipse (- 10,-10,40,40 def timerevent (self,qtimerevent): Self.flash = Not flash self.update () class StarI TEM (qgraphicsitem): def __init__ (self): suPer (staritem,self). __init__ () Self.fix = Qpixmap () self.fix.load ("Image/star.png") def Boundingre CT (self): return QRECTF (-self.fix.width ()/2,-self.fix.height ()/2,self.fix.width (), Self.fix.height ()) def Paint (Self,painter,option,widget): Painter.drawpixmap (Self.boundingrect (). TopLeft (), Self.fix) cl Ass MainWindow (qmainwindow): def __init__ (Self,qwidget): Super (Mainwindow,self). __init__ () self.create Actions () Self.createmenus () Self.scene = Qgraphicsscene (self) Self.scene.setScen Erect ( -200,-200,600,600) self.initscene () Self.view = Qgraphicsview () self.view.setScene ( Self.scene) self.view.setMinimumSize (600,600) self.view.show () Self.setcentralwidget (self . View) Self.resize (800,600) self.setwindowtitle (self.tr ("various graphics Items") def createactions (SE LF): Self.newact = Qaction (self.tr ("New"), self) self.connect (self.newact,signal ("triggered ()"), self.slotnew) self
        
        . clearact = Qaction (self.tr ("clear"), self) self.connect (self.clearact,signal ("triggered ()"), Self.slotcrear) Self.exitact = Qaction (self.tr ("Exit"), self) self.addellipseitemact = Qaction (self.tr ("Add Ellipse"), SE LF) Self.addpolygonitemact = qaction (self.tr ("Add Polygon"), self) self.addtextitemact = Qaction (self.tr ("A DD Text "), self) self.addflashitemact = Qaction (self.tr (" Add Flash "), self) self.addrectitemact = qaction (SE lf.tr ("Add Rectangle"), self) self.addanimitemact = Qaction (self.tr ("Add Animation"), self) self.addalphaite MacT = Qaction (self.tr ("Add alpha-png"), self) def createmenus (self): Filemenu = Self.menubar (). AddMenu (SE lf.tr ("File")) filemenu.addaction (self.newact) filemenu.addaction (self.clearact) filemenu.addactio
        N (self.exitact)Filemenu.addaction (self.addellipseitemact) filemenu.addaction (self.addpolygonitemact) fileMenu.addAction (s
        Elf.addtextitemact) filemenu.addaction (self.addflashitemact) filemenu.addaction (self.addrectitemact)
        Filemenu.addaction (self.addanimitemact) filemenu.addaction (self.addalphaitemact) def initScene (self): For I in range (3): Self.slotaddflashitem () to I in range (3): Self.slotaddellipsei TEM () for I in Range (3): Self.slotaddrectitem () to I in range (3): Self.slotaddalp Haitem () for I in Range (3): Self.slotaddpolygonitem () to I in range (3): Self.slot
        Addtextitem () for I in Range (3): Self.slotaddanimationitem () def slotnew (self): Self.slotcrear () self.initscene () Newwin = MainWindow (self) newwin.show () def slotcre AR (self): listitEM = Self.scene.items () while (Len (ListItem)!= 0): Self.scene.removeItem (listitem[0]) list
        Item.remove (Listitem[0]) def slotaddellipseitem (self): item = Qgraphicsellipseitem (QRECTF (0,0,80,60)) Item.setpen (Qpen (Qt.nopen)) Item.setbrush (Qcolor (Qrand ()%256,qrand ()%256,qrand ()%256)) scale = ((Qran D ()%10) +1/5.0 item.scale (Scale,scale) Item.setflag (qgraphicsitem.itemismovable) self.scene.addIte M (item) Item.setpos (Qrand ()%int (Self.scene.sceneRect (). Width ())--(Qrand ()%int (Self.scene.sceneRect (). Heigh T ())-def Slotaddpolygonitem (self): v = [qpointf (30,-15), qpointf (0,-30), qpointf (30,-15), qpointf (30 , -15), qpointf (0,30), qpointf (30,15)] item = Qgraphicspolygonitem (qpolygonf (v)) Item.setbrush (Qcolor (Qrand)
        %256,qrand ()%256,qrand ()%256)) Item.setflag (qgraphicsitem.itemismovable) Self.scene.addItem (item) Item.setpos (Qrand ()%int (Self.scene.sceneRect (). Width ())--(Qrand ()%int (Self.scene.sceneRect (). Height ())) def Slotaddrecti TEM (self): item = Qgraphicsrectitem (QRECTF (0,0,60,60)) pen = Qpen () pen.setwidth (3) pen.se TColor (Qcolor (Qrand ()%256,qrand ()%256,qrand ()%256)) Item.setpen (pen) item.setbrush (Qcolor (Qrand ()%256,qran D ()%256,qrand ()%256)) Item.setflag (qgraphicsitem.itemismovable) Self.scene.addItem (item) ITEM.SETP Os ((Qrand ()%int (Self.scene.sceneRect (). Width ())--(Qrand ()%int (Self.scene.sceneRect (). Height ())) def Slotaddtextitem (self): Font = Qfont ("Times", item = Qgraphicstextitem ("Hello Qt") Item.setfont ( Font) Item.setflag (qgraphicsitem.itemismovable) Item.setdefaulttextcolor (Qcolor (Qrand ()%256,qrand ()%256,QR and ()%256) Self.scene.addItem (item) Item.setpos (Qrand ()%int (Self.scene.sceneRect (). Width ()))-(Qran D ()%int (self.scene.sceneRect (). Height ())-def Slotaddflashitem (self): item = Flashitem (QObject) scale = ((Qrand ()% 1)/5.0 Item.scale (Scale,scale) Self.scene.addItem (item) Item.setpos (Qrand ()%int (Self.scene.
        Scenerect (). Width ())--(Qrand ()%int (Self.scene.sceneRect (). Height ())) def slotaddalphaitem (self):
        Item = Self.scene.addPixmap (Qpixmap ("Image/butterfly.png")) Item.setflag (qgraphicsitem.itemismovable) Item.setpos ((Qrand ()%int (Self.scene.sceneRect (). Width ())--(Qrand ()%int (Self.scene.sceneRect (). Height ()))- def slotaddanimationitem (self): item = Staritem () Anim = Qgraphicsitemanimation () an Im.setitem (item) TimeLine = Qtimeline (4000) timeline.setcurveshape (Qtimeline.sinecurve) timeline.s
            Etloopcount (0) anim.settimeline (timeLine) y = (Qrand ()%800)-for I in range (800): Anim.setposat (i/800.0,qpointf (i -600,y) Timeline.start () Self.scene.addItem (item) If __name__ = = ' __main__ ': import sys app = Qapplication (sys.argv) MainWindow = MainWindow (qwidget) mainwindow.show () Sys.exit (App.exec_ ())


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.