Issue
I'm facing a problem that I cannot solve, even if it shouldn't be difficult.
I have a QListWidget
with 5 items (so 5 rows) and 2 related QStackedWidgets
. The first made of 3 pages: the first page of it contains another nested QStackedWidgets
with 2 pages. So a total pf 5 pages.
A small gif to have an idea:
Now what I'm trying to do is to create a function that links the QListWidget
and the 2 QStackedWidget
. I would like that the item 1 and 2 of the QListWidget
were linked only to the first page of the parent QStackedWidget
and the first and second page of the nested QStackedWidget
while item 3, 4 and 5 were linked only to the parent QStackedWidget
(2nd, 3rd and 4th page).
The piece of code I wrote is the following:
# connect the listWidget currentRow signal to a custom function
self.listWidget.currentRowChanged.connect(self.updateStacked)
def updateStacked(self):
idx = self.listWidget.currentRow()
if idx is 0 or idx is 1:
self.stackedPlotWidget.setCurrentIndex(0)
self.stackedNestedWidget.setCurrentIndex(1)
else:
self.stackedPlotWidget.setCurrentIndex(idx - 1)
but something is not working correctly: for QlistWidget
rows 0 and 1 works nice but it seems not taking into account the else statement.
Some suggestions?
Solution
The correct way to write the slot is like this:
self.listWidget.currentRowChanged.connect(self.updateStacked)
def updateStacked(self, row):
if 0 <= row <= 1:
self.stackedPlotWidget.setCurrentIndex(0)
self.stackedNestedWidget.setCurrentIndex(row)
elif row > 1:
self.stackedPlotWidget.setCurrentIndex(row - 1)
UPDATE:
Below is a complete example showing how to handle all the signals. Note that the spin-box is only there to mimic setting the indexes of the stack-widgets programmatically.
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.stack1 = QtGui.QStackedWidget()
self.stack2 = QtGui.QStackedWidget()
for text in 'ONE', 'TWO':
page = QtGui.QWidget(self.stack2)
layout = QtGui.QVBoxLayout(page)
layout.addWidget(QtGui.QLabel(text))
self.stack2.addWidget(page)
page = QtGui.QWidget(self.stack1)
layout = QtGui.QVBoxLayout(page)
layout.addWidget(QtGui.QLabel('TOP'))
layout.addWidget(self.stack2)
layout.addWidget(QtGui.QLabel('BOTTOM'))
self.stack1.addWidget(page)
for text in 'THREE', 'FOUR', 'FIVE':
page = QtGui.QWidget(self.stack1)
layout = QtGui.QVBoxLayout(page)
layout.addWidget(QtGui.QLabel(text))
self.stack1.addWidget(page)
self.listWidget = QtGui.QListWidget()
self.listWidget.addItems('ONE TWO THREE FOUR FIVE'.split())
self.listWidget.currentRowChanged.connect(self.updateStacked)
self.spin = QtGui.QSpinBox()
self.spin.setRange(0, 4)
self.spin.valueChanged.connect(self.updateStacked)
layout = QtGui.QHBoxLayout(self)
layout.addWidget(self.listWidget)
layout.addWidget(self.stack1)
layout.addWidget(self.spin)
self.stack1.currentChanged.connect(self.updateList)
self.stack2.currentChanged.connect(self.updateList)
def updateStacked(self, row):
if 0 <= row <= 1:
self.stack1.setCurrentIndex(0)
self.stack2.setCurrentIndex(row)
elif row > 1:
self.stack1.setCurrentIndex(row - 1)
def updateList(self, index):
self.listWidget.blockSignals(True)
if self.sender() is self.stack1:
index += (1 if index >= 1 else self.stack2.currentIndex())
self.listWidget.setCurrentRow(index)
self.listWidget.blockSignals(False)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 50, 300, 50)
window.show()
sys.exit(app.exec_())
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.