PyQt实现界面翻转切换效果
程序员文章站
2023-10-18 20:31:15
pyqt实现界面翻转切换效果是用qt的场景功能来实现的,用到了qgraphicsview,qgraphicslinearlayout,qgraphicswidget等有关q...
pyqt实现界面翻转切换效果是用qt的场景功能来实现的,用到了qgraphicsview,qgraphicslinearlayout,qgraphicswidget等有关qt场景的库。算是对qt场景的一个小小的尝试,涉及内容不深,程序效果并是随心所欲,需要去进一步的改善和提高。暂且先把代码贴在此处,供大家学习和指正。
工程包括四个类:
界面a,testmainwindow,用来充当翻转效果的a面。
界面b,testmainwindowtwo,用来充当翻转效果的b面。
绘图界面:testgraphicwidget,用来绘制界面a和b。
主界面:mainwindow,是一个全屏的透明窗口,是整个效果展现的总舞台,内部包含一个qgraphicsscene和一个qgraphicsview,用来展示效果中的界面翻转和界面替换。
整个效果的原理总结为几点:
首先,将整个效果需要的所有界面添加到testgraphicwidget中,在将testgraphicwidget放入到qgraphicsscene中,然后经qgraphicsscene添加到主界面中。
然后,界面切换实现,两个函数,非常简单,要显示a,就把b移除并隐藏,要显示b,则把a移除并隐藏。
def setone(self): self.twowidget.hide() self.onewidget.show() self.layout.removeitem(self.twotestwidget) self.layout.additem(self.onetestwidget) self.view.update() def settwo(self): self.onewidget.hide() self.twowidget.show() self.layout.removeitem(self.onetestwidget) self.layout.additem(self.twotestwidget) self.view.update()
然后是最重要的,翻转效果的实现,用的是testgraphicwidget特有的翻转方法,参数可以根据实景情况调整。
def transeformr(self,count): r = self.form.boundingrect() for i in range(1,count): self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(91.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() self.view.update() self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(270 - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.view.update() if self.formflag %2 == 0: self.setone() else: self.settwo() for i in range(1,count): self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(270 + 93.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() self.view.update()
而且提供了两种让程序等待但界面不会卡死的方法:
def sleep(self,msec): dietime = qtime.currenttime().addmsecs(msec) print dietime,qtime.currenttime() #a = 0 while( qtime.currenttime() < dietime ): #print "000000000000" qcoreapplication.processevents(qeventloop.allevents, 100) def waitmethod(self): tt = qelapsedtimer() tt.start() q = qeventloop() t = qtimer() t.setsingleshot(true) self.connect(t, signal("timeout()"), q.quit) t.start(1) # 5s timeout q.exec_() if(t.isactive()): t.stop() else: pass print tt.elapsed()
下面粘上源码,供参考,这个源码可以直接运行,内部的调试信息可以忽略:
#coding:utf-8 ''''' created on 2015 7 15 @author: guowu ''' from pyqt4.qtgui import qwidget, qtextedit, qpushbutton, qgraphicsscene,\ qgraphicswidget, qgraphicslinearlayout, qgraphicsview, qapplication,\ qtransform, qhboxlayout, qpainter, qlabel, qgraphicslayoutitem, qfont,\ qpixmap, qbrush from pyqt4.qtcore import qt, qtime, qcoreapplication, qeventloop, qobject,\ signal, qpoint, qtimer, qbasictimer, qelapsedtimer, qpointf import sys import time class testgraphicwidget(qgraphicswidget): def __init__(self,parent=none): super(testgraphicwidget,self).__init__(parent) self.setwindowflags(qt.window) self.setwindowtitle("turn widget") self.resize(400,400) #self.setpos(qpoint(0,0)) self.mousepressed = false def closeevent(self,event): print "closeclosetest" self.emit(signal("startturn")) def mousemoveevent(self, event): print "move move" if self.mousepressed: #self.move(self.pos() + event.pos() - self.currentpos) self.setpos(self.pos() + event.pos() - self.currentpos) def mousepressevent(self, event): if event.buttons() == qt.leftbutton: self.currentpos = event.pos() self.mousepressed = true class testmainwindow(qwidget): def __init__(self,parent=none): super(testmainwindow,self).__init__(parent) #self.setstylesheet("background: transparent;border:0px;") self.setattribute(qt.wa_translucentbackground,true) self.firstbutton = qpushbutton(u"翻转") self.secondbutton = qpushbutton(u"翻转") self.thirdbutton = qpushbutton(u"翻转") self.mainlayout = qhboxlayout(self) self.mainlayout.addwidget(self.firstbutton) self.mainlayout.addwidget(self.secondbutton) self.mainlayout.addwidget(self.thirdbutton) self.connect(self.firstbutton, signal("clicked()"), self.startturn) self.connect(self.secondbutton, signal("clicked()"), self.startturn) self.connect(self.thirdbutton, signal("clicked()"), self.startturn) def startturn(self): self.emit(signal("buttonclicked")) def closeevent(self,event): print "closeclosetest" self.emit(signal("startturn")) def paintevent(self,event): #print "paintevent" painter = qpainter(self) painter.setrenderhint(qpainter.smoothpixmaptransform, true)#像素光滑 painter.setrenderhint(qpainter.antialiasing, true)#反锯齿 pix = qpixmap("cloud-bak.jpg").scaled(self.width(),self.height()) painter.setbrush(qbrush(pix)) painter.drawroundrect(self.rect(),5,5) class testmainwindowtwo(qwidget): def __init__(self,parent=none): super(testmainwindowtwo,self).__init__(parent) #self.setstylesheet("qwidget{background: transparent;border:0px;}") self.setattribute(qt.wa_translucentbackground,true) self.firstbutton = qpushbutton(u"p翻转") self.secondbutton = qpushbutton(u"p翻转") self.thirdbutton = qpushbutton(u"p翻转") self.mainlayout = qhboxlayout(self) self.mainlayout.addwidget(self.firstbutton) self.mainlayout.addwidget(self.secondbutton) self.mainlayout.addwidget(self.thirdbutton) self.connect(self.firstbutton, signal("clicked()"), self.startturn) self.connect(self.secondbutton, signal("clicked()"), self.startturn) self.connect(self.thirdbutton, signal("clicked()"), self.startturn) def startturn(self): self.emit(signal("buttonclicked")) def paintevent(self,event): #print "paintevent" painter = qpainter(self) painter.setrenderhint(qpainter.smoothpixmaptransform, true)#像素光滑 painter.setrenderhint(qpainter.antialiasing, true)#反锯齿 pix = qpixmap("login.jpg").scaled(self.width(),self.height()) painter.setbrush(qbrush(pix)) painter.drawroundrect(self.rect(),5,5) class mainwindow(qwidget): def __init__(self,parent=none): super(mainwindow,self).__init__(parent) #self.setstylesheet("qgraphicsview{background:rgb(0,0,0,0);border:0px;}") self.formflag = 0 self.scene = qgraphicsscene() self.setwindowflags(qt.framelesswindowhint|qt.windowstaysontophint) self.setattribute(qt.wa_translucentbackground,true) #创建部件,并关联它们的信号和槽 self.onewidget = testmainwindow() self.connect(self.onewidget, signal("buttonclicked"),self.startturn) self.twowidget = testmainwindowtwo() self.connect(self.twowidget, signal("buttonclicked"),self.startturn) #self.textedit = qgraphicslayoutitem(self.edit) self.onetestwidget = self.scene.addwidget(self.onewidget) self.twotestwidget = self.scene.addwidget(self.twowidget) self.form = testgraphicwidget() self.connect(self.form, signal("startturn"),self.close) #将部件添加到布局管理器中 self.layout = qgraphicslinearlayout(self.form) self.layout.setspacing(0) self.layout.additem(self.onetestwidget) self.layout.additem(self.twotestwidget) self.layout.removeitem(self.twotestwidget) self.twowidget.hide() #创建图形部件,设置其为一个顶层窗口,然后在其上应用布局 #self.form.setwindowflags(qt.window|qt.framelesswindowhint) #self.form.setwindowtitle("widget item") #self.form.setlayout(layout) self.scene.additem(self.form) #self.form.setpos(qpointf(0,0)) #self.form.hide() self.view = qgraphicsview(self.scene,self) #self.view.setscene(self.scene) self.view.setrenderhint(qpainter.antialiasing) self.view.setviewportupdatemode(qgraphicsview.boundingrectviewportupdate) self.view.resize(qapplication.desktop().width(),qapplication.desktop().height()) self.view.setstylesheet("background: transparent;border:0px;") self.view.setwindowflags(qt.framelesswindowhint) self.view.sethorizontalscrollbarpolicy(qt.scrollbaralwaysoff) self.view.setverticalscrollbarpolicy(qt.scrollbaralwaysoff) self.view.move(qpoint(0,0)) #self.view.setattribute(qt.wa_translucentbackground,true) #self.form.resize(500,500) #self.form.setwindowflags(qt.framelesswindowhint) #for(int i=1;i<=360;i++) def setone(self): self.twowidget.hide() self.onewidget.show() self.layout.removeitem(self.twotestwidget) self.layout.additem(self.onetestwidget) self.view.update() def settwo(self): self.onewidget.hide() self.twowidget.show() self.layout.removeitem(self.onetestwidget) self.layout.additem(self.twotestwidget) self.view.update() def transeformt(self,count): r = self.form.boundingrect() for i in range(1,count): print "............." self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(364.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() #self.sleep(1) #time.sleep(1) self.view.update() # def transeforms(self,count): r = self.form.boundingrect() for i in range(1,count): print "............." self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(182.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() self.view.update() def transeformr(self,count): r = self.form.boundingrect() for i in range(1,count): print "............." self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(91.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() self.view.update() self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(270 - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.view.update() if self.formflag %2 == 0: self.setone() else: self.settwo() for i in range(1,count): self.form.settransform(qtransform() .translate(r.width() / 2, r.height() / 2) .rotate(270 + 93.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width() / 2, -r.height() / 2)) self.waitmethod() self.view.update() def transeformb(self,count): r = self.form.boundingrect() for i in range(1,count): print "............." self.form.settransform(qtransform() .translate(r.width(), r.height()) .rotate(91.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width(), -r.height())) self.waitmethod() self.view.update() self.form.settransform(qtransform() .translate(r.width(), r.height()) .rotate(270 - 360 * 1, qt.yaxis) .translate(-r.width(), -r.height())) self.view.update() for i in range(1,count): self.form.settransform(qtransform() .translate(r.width(), r.height()) .rotate(270 + 93.00/count*i - 360 * 1, qt.yaxis) .translate(-r.width(), -r.height())) self.waitmethod() self.view.update() def transeform(self): print self.form.pos() #self.scene.itemat(qpointf) rxx = self.scene.itemsboundingrect() rx = self.form.boundingrect() r = self.form.geometry() print r,rx,rxx for i in range(1,361): print self.form.pos() print "............." #print r.width(),r.height() transform = qtransform() transform.translate(r.width() / 2, r.height()/2)#中心点,原点 transform.rotate(i - 360 * 1, qt.yaxis)#绕x轴旋转角度 self.form.settransform(transform) # self.form.settransform(qtransform() # .translate(r.width() / 2, r.height() / 2) # .rotate(i - 360 * 1, qt.yaxis) # .translate(-r.width() / 2, -r.height() / 2)) # self.form.settransform(qtransform() # .translate(250, 250) # .rotate(i - 360 * 1, qt.yaxis) # .translate(-250, -250)) self.waitmethod() self.view.update() # def startturn(self): self.formflag += 1 self.transeformr(30) #self.transeform() #self.form.close() #self.view.close() def closeevent(self,event): print "close" self.form.close() self.view.close() self.close() def sleep(self,msec): dietime = qtime.currenttime().addmsecs(msec) print dietime,qtime.currenttime() #a = 0 while( qtime.currenttime() < dietime ): #print "000000000000" qcoreapplication.processevents(qeventloop.allevents, 100) def waitmethod(self): tt = qelapsedtimer() tt.start() q = qeventloop() t = qtimer() t.setsingleshot(true) self.connect(t, signal("timeout()"), q.quit) t.start(1) # 5s timeout q.exec_() if(t.isactive()): t.stop() else: pass print tt.elapsed() if __name__ == "__main__": app = qapplication(sys.argv) font = qfont() font.setpointsize(16) font.setfamily(("roman times")) app.setfont(font) c = mainwindow() c.show() c.move(qpoint(0,0)) app.exec_()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。