Я добавляю ряды виджетов в QGridLayout
вот так (с помощью кнопки):
def ajouter_un_mot_vocab(self) :
'''
'''
# Dictionnaire des mots de vocabulaire
self.dico_vocab_mot = {}
# Dictionnaire des définitions des mots de vocabulaire
self.dico_vocab_def = {}
# Liste pour chargement des données
# (écriture des textes par l'utilisateur)
self.liste_mots_vocabulaire = []
print
print 'self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.rowCount() creation', self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.rowCount()
print
#
for r in range(self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.rowCount()) :
# Création des widgets et taille générique
self.dico_vocab_mot[r] = QTextEdit()
self.dico_vocab_def[r] = QTextEdit()
self.dico_vocab_mot[r].setMaximumWidth(180)
self.dico_vocab_mot[r].setMinimumWidth(180)
self.dico_vocab_mot[r].setMaximumHeight(54)
self.dico_vocab_mot[r].setMinimumHeight(54)
self.dico_vocab_def[r].setMaximumHeight(54)
self.dico_vocab_def[r].setMinimumHeight(54)
print 'r', r
# Conditions de redimensionnement
if r > 5 :
self.dico_vocab_mot[r].setMaximumHeight(34)
self.dico_vocab_mot[r].setMinimumHeight(34)
self.dico_vocab_def[r].setMaximumHeight(34)
self.dico_vocab_def[r].setMinimumHeight(34)
# Répartition dans la grille
self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.addWidget(self.dico_vocab_mot[r], r+1, 0)
self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.addWidget(self.dico_vocab_def[r], r+1, 1)
# Ecriture des n°s de lignes dans la partie mots de vocabulaire
self.grilledef supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
stack_3.addWidget(self.dico_vocab_mot[r].setText(str(r+1)+'. '), r+1, 0)
# Les données sont introduites dans une liste
self.liste_mots_vocabulaire.append([self.dico_vocab_mot[r], self.dico_vocab_def[r]])
# =====================================================
# Signaux
self.dico_vocab_mot[r].textChanged.connect(self.changements_phase_3)
self.dico_vocab_def[r].textChanged.connect(self.changements_phase_3)
# =====================================================
print 'self.dico_vocab_mot', self.dico_vocab_mot
print 'self.dico_vocab_def', self.dico_vocab_def
print self.liste_mots_vocabulaire
И удаление рядов виджетов вот так:
def supprimer_un_mot_vocab(self) :
'''
'''
index = len(self.liste_mots_vocabulaire)-1
for r in reversed(range(self.grille_3_stack_3.rowCount())) :
for c in reversed(range(self.grille_3_stack_3.columnCount()))
layout = self.grille_3_stack_3.itemAtPosition(r, c)
if layout is not None :
layout.widget().deleteLater()
#layout_1.widget().hide()
self.grille_3_stack_3.removeItem(layout)
self.liste_mots_vocabulaire.pop()
del self.dico_vocab_mot[index]
del self.dico_vocab_def[index]
print
print "rowCount apres suppr", self.grille_3_stack_3.rowCount()
print
print self.dico_vocab_mot
print self.dico_vocab_def
print self.liste_mots_vocabulaire
print
Все работает хорошо с первого раза, когда я добавляю ряды виджетов (например, пять рядов виджетов). Все тоже хорошо работает, если я удаляю строки (например, две строки виджетов). Но как только я решаю снова добавить строку (после удаления), она не работает должным образом: у меня получается шесть строк, а должно быть три.
Должно быть, это мой код для удаления строк, который не работает должным образом (хотя строки удаляются визуально). Что я делаю не так?