Issue
I found a great example on how to do a fade between two widgets. Now, what I'm trying to do here, is to split every widgets I build in their own classes. The problem with the code at this stage is in the editor_ class, the line:
self.buttonBox.rejected.connect(stack.setPage1)
When I click on "Cancel" button, it gives me these results :
# Result: Traceback (most recent call last):
File "<string>", line 28, in paintEvent
AttributeError: 'FaderWidget' object has no attribute 'pixmap_opacity'
Is it some kind of parenting error?
Thank you
The code:
import sys
from PySide.QtCore import QTimeLine
from PySide.QtGui import *
class FaderWidget(QWidget):
def __init__(self, old_widget, new_widget):
QWidget.__init__(self, new_widget)
self.old_pixmap = QPixmap(new_widget.size())
old_widget.render(self.old_pixmap)
self.pixmap_opacity = 1.0
self.timeline = QTimeLine()
self.timeline.valueChanged.connect(self.animate)
self.timeline.finished.connect(self.close)
self.timeline.setDuration(333)
self.timeline.start()
self.resize(new_widget.size())
self.show()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setOpacity(self.pixmap_opacity)
painter.drawPixmap(0, 0, self.old_pixmap)
painter.end()
def animate(self, value):
self.pixmap_opacity = 1.0 - value
self.repaint()
class StackedWidget(QStackedWidget):
def __init__(self, parent = None):
QStackedWidget.__init__(self, parent)
def setCurrentIndex(self, index):
self.fader_widget = FaderWidget(self.currentWidget(), self.widget(index))
QStackedWidget.setCurrentIndex(self, index)
def setPage1(self):
self.setCurrentIndex(0)
def setPage2(self):
self.setCurrentIndex(1)
def setPage3(self):
self.setCurrentIndex(2)
def setPage4(self):
self.setCurrentIndex(3)
class editor_(QWidget):
"""build a text editor"""
def __init__(self):
super(editor_, self).__init__()
editor = QTextEdit()
editor.setPlainText("Kawabungaaaa! "*100)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(stack.setPage1)
layout = QVBoxLayout()
layout.addWidget(editor)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
class mainWidget(QWidget):
def __init__(self):
super(mainWidget, self).__init__()
self.setMinimumSize(500,300)
widget1 = QWidget()
widget2 = QCalendarWidget()
widget3 = QListView()
widget4 = editor_()
grid = QGridLayout()
allPlates = QPushButton("Calendar")
singlePlate = QPushButton("ListView")
allRoto = QPushButton("Editor")
allLighting = QPushButton("Buttons")
grid.addWidget(allPlates,0,0)
grid.addWidget(singlePlate,0,1)
grid.addWidget(allRoto,1,0)
grid.addWidget(allLighting,1,1)
widget1.setLayout(grid)
stack = StackedWidget()
stack.addWidget(widget1)
stack.addWidget(widget2)
stack.addWidget(widget3)
stack.addWidget(widget4)
layout = QHBoxLayout()
layout.addWidget(stack)
self.setLayout(layout)
allPlates.clicked.connect(stack.setPage2)
singlePlate.clicked.connect(stack.setPage3)
allRoto.clicked.connect(stack.setPage4)
allLighting.clicked.connect(stack.setPage1)
a = mainWidget()
a.show()
Solution
You can not use a resource but this is not within the current environment. In your case, the stack is not defined within the editor_ class, so it will generate an error. To solve it you must connect after creating them and in the field they have in common, in your case mainWidget
# ...
stack.addWidget(widget4)
widget4.buttonBox.rejected.connect(stack.setPage1)
# ...
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.