问题描述
在应用程序中,我创建像这样的小部件行(有一个用于创建该行的按钮,另一个是用于删除该小部件的按钮):
In a application I create lines of widgets like this (there is a button to create the line and another to remove the widget):
创建小部件行的代码:
def ajouter_ref_artistique(self) :
'''
Gestion des widgets (AJOUTER widgets) partie Références artistiques
'''
# Nombre d'éléments présents
try :
# ...
r = len(self.l_t_ref_art)
except :
# ...
r = len(self.liste_ref_artistiques)
# Création des QTextEdit
self.dico_chem_ref_art[r] = QTextEdit()
self.dico_com_ref_art[r] = QTextEdit()
self.fonte(self.dico_com_ref_art[r]) # Fontion pout l'attribution de la fonte
self.dico_chem_ref_art[r].setMaximumWidth(150)
self.dico_chem_ref_art[r].setMinimumWidth(150)
self.dico_chem_ref_art[r].setMaximumHeight(84)
self.dico_chem_ref_art[r].setMinimumHeight(84)
self.dico_com_ref_art[r].setMaximumWidth(430)
self.dico_com_ref_art[r].setMinimumWidth(430)
self.dico_com_ref_art[r].setMaximumHeight(84)
self.dico_com_ref_art[r].setMinimumHeight(84)
# Création des boutons de chargement
self.dico_bout_charg_ref_art[r] = QPushButton("Ouvrir référence art. {}".format(r+1))
self.dico_bout_charg_ref_art[r].setMaximumWidth(180)
self.dico_bout_charg_ref_art[r].setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #6190F2);border-style: solid;border-width: 2px;border-radius: 8px;border-color: #9BB7F0;padding: 2px")
# Répartition dans la grille
self.grille_3_stack_6.addWidget(self.dico_chem_ref_art[r], r, 0)
self.grille_3_stack_6.addWidget(self.dico_com_ref_art[r], r, 1)
self.grille_3_stack_6.addWidget(self.dico_bout_charg_ref_art[r], r, 2)
# Ecriture des n°s de lignes
self.dico_chem_ref_art[r].setText(str(r+1)+'. ')
# Placement en lecture seule (vignette de l'oeuvre et chemin)
self.dico_chem_ref_art[r].setReadOnly(True)
try :
# ...
self.l_t_ref_art.append([self.dico_chem_ref_art[r], self.dico_com_ref_art[r], r])
except :
# ...
# id 0 --> self.dico_chem_ref_art[r] (QTextEdit pour récup des données et affichage vignette image)
# id 1 --> self.dico_com_ref_art[r] (QTextEdit pour récup des données commentaires oeuvres)
# id 2 --> r (Chiffre correspondant au numéro du bouton)
self.liste_ref_artistiques.append([self.dico_chem_ref_art[r], self.dico_com_ref_art[r], r])
# =====================================================
# Signaux
# ---------- Récup des données textuelles
self.dico_chem_ref_art[r].textChanged.connect(self.changements_phase_6)
self.dico_com_ref_art[r].textChanged.connect(self.changements_phase_6)
# ---------- Récup du libellé du bouton sélectionné par l'utilisateur
self.dico_bout_charg_ref_art[r].released.connect(self.libelle_bouton_ref_art)
# =====================================================
用于删除小部件行的代码:
Code for removing widget lines :
def supprimer_ref_artistique(self) :
'''
Gestion des widgets (SUPPRIMER widgets) partie Références artistiques
'''
try :
# Dans le cas du chargement d'une séquence prof
row = len(self.l_t_ref_art) - 1
del self.l_t_ref_art[row]
# ...
self.row_ref_art = row
except :
# Dans le cas de la 1ère création d'une séquence prof
row = len(self.liste_ref_artistiques) - 1
del self.liste_ref_artistiques[row]
# ...
self.row_ref_art = row
if row >= 0:
for column in range(self.grille_3_stack_6.columnCount()):
item = self.grille_3_stack_6.itemAtPosition(row, column)
#item = self.grille_3_stack_6.itemAtPosition(1, column)
if item is not None:
item.widget().deleteLater()
del self.dico_chem_ref_art[row]
del self.dico_com_ref_art[row]
del self.dico_bout_charg_ref_art[row]
我也想使用向上"和向下"按钮更改小部件行的位置(向上或向下),我不知道该怎么做.
I would also like to change the position of the widgets lines (up or down) with 'up' and 'down' buttons, I don't know how to do it.
推荐答案
您可以使用QListWidget存储行.
You can use a QListWidget to store your rows.
Qt对于将QWidget放置为QListWidget中的项有一些困难的语法.
Qt has some difficult syntax for putting a QWidget as an item in a QListWidget.
对于简单的文本,您可以按照这篇文章上移项目:
For simple text, you can follow this post to move items up:
currentRow = self.listWidget.currentRow()
currentItem = self.listWidget.takeItem(currentRow)
self.listWidget.insertItem(currentRow - 1, currentItem)
和向下移动项目的类似过程.当行中有小部件时,它会涉及更多的内容.请参阅下面的完整示例.
And a similar process to move items down. When you have widgets in your rows, it gets a lot more involved. See complete example below.
import sys
import random
from PyQt5.QtWidgets import (QWidget, QPushButton, QLabel, QWidget,
QHBoxLayout, QVBoxLayout, QApplication, QListWidget, QTextEdit,
QListWidgetItem, QLayout, QLineEdit)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
addButton = QPushButton('+')
addButton.clicked.connect(self.addrow)
delButton = QPushButton('-')
delButton.clicked.connect(self.delrow)
upButton = QPushButton('▲', parent = self)
upButton.clicked.connect(self.rowup)
downButton = QPushButton('▼', parent = self)
downButton.clicked.connect(self.rowdown)
hbox = QHBoxLayout()
hbox.addWidget(addButton)
hbox.addWidget(delButton)
hbox.addWidget(upButton)
hbox.addWidget(downButton)
self.listbox = QListWidget()
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(self.listbox)
vbox.setStretch(0,1)
vbox.setStretch(1,4)
self.setLayout(vbox)
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle('Test')
self.show()
def rowup(self):
row_num = self.listbox.currentRow()
if row_num > 0:
row = self.listbox.itemWidget(self.listbox.currentItem())
itemN = self.listbox.currentItem().clone()
self.listbox.insertItem(row_num -1, itemN)
self.listbox.setItemWidget(itemN, row)
self.listbox.takeItem(row_num+1)
self.listbox.setCurrentRow(row_num-1)
def rowdown(self):
row_num = self.listbox.currentRow()
if row_num == -1:
# no selection. abort
return
elif row_num < self.listbox.count():
row = self.listbox.itemWidget(self.listbox.currentItem())
itemN = self.listbox.currentItem().clone()
self.listbox.insertItem(row_num + 2, itemN)
self.listbox.setItemWidget(itemN, row)
self.listbox.takeItem(row_num)
self.listbox.setCurrentRow(row_num+1)
def addrow(self):
row = self.makerow()
itemN = QListWidgetItem()
itemN.setSizeHint(row.sizeHint())
self.listbox.addItem(itemN) # add itemN to end of list. use insertItem
# to insert in specific location
self.listbox.setItemWidget(itemN, row)
def delrow(self):
if self.listbox.currentRow() == -1:
# no selection. delete last row
row_num = self.listbox.count() - 1
else:
row_num = self.listbox.currentRow()
item = self.listbox.takeItem(row_num)
del item
def makerow(self):
widget = QWidget()
hbox = QHBoxLayout()
r = random.random()
r = '%f' % r
print(r)
label = QLabel(r)
textedit = QLineEdit()
button = QPushButton('Ouvrir reference art %s' % r)
hbox.addWidget(label)
hbox.addWidget(textedit)
hbox.addWidget(button)
hbox.addStretch()
hbox.setSizeConstraint(QLayout.SetFixedSize)
widget.setLayout(hbox)
return widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
这篇关于小部件的上下行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!