【发布时间】:2018-05-03 00:02:44
【问题描述】:
我正在向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.grille_3_stack_3.rowCount() creation', self.grille_3_stack_3.rowCount()
print
#
for r in range(self.grille_3_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.grille_3_stack_3.addWidget(self.dico_vocab_mot[r], r+1, 0)
self.grille_3_stack_3.addWidget(self.dico_vocab_def[r], r+1, 1)
# Ecriture des n°s de lignes dans la partie mots de vocabulaire
self.grille_3_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
当我添加多行小部件(例如五行小部件)时,第一次一切正常。如果我删除行(例如两行小部件),一切也都很好。但是一旦我决定再次添加一行(删除后),它就无法正常工作:我最终得到了 6 行,而我应该有 3 行。
它必须是我的代码,用于删除不能正常工作的行(但这些行在视觉上被删除了)。我做错了什么?
【问题讨论】:
-
如果你想让我们帮助你,你必须提供一个Minimal, Complete, and Verifiable example,也就是说它可以被复制但是你需要更多的代码来执行它,我个人懒于完成你的代码
-
您好 eyllanesc,对不起,我正在尝试尽快发送更完整的答案。这是我在这里的第一条消息(我想发布完整的 689 行代码,但在代码标签中,......不太好......)。
-
您必须提供一个最小、完整和可验证的示例。如果您的代码很长,请创建另一个项目,其中仅包含生成错误的功能。
标签: python layout pyqt pyqt4 qgridlayout