class ItemWidget(QWidget):
def __init__(self, id_str="", parent=None):
super(ItemWidget, self).__init__(parent)
self.id_str = id_str
self._generateUI()
def _generateUI(self):
main_layout = QGridLayout()
self.setLayout(main_layout)
title = QLabel("title" + self.id_str)
main_layout.addWidget(title, 0, 0, 1, 3)
close_button = QPushButton("-")
close_button.setFixedWidth(30)
close_button.clicked.connect(self._close_widget) # add to close the widget
main_layout.addWidget(close_button, 0, 3, 1, 1)
spinbox = QSpinBox()
main_layout.addWidget(spinbox, 1, 0, 1, 4)
def _close_widget(self):
self.deleteLater() # main function to close widget
* There are many ways to remove widget from the parent layout. For example, some people use setParent(None) or something. However don’t use removeWidget() because it just removes widget without destroying it. Please refer to the discussion below.
I don’t know why but adjustSize() doesn’t work correctly until some events are processed in the event loop. So I called QApplication.processEvents() in the for loop. Please let me know if you have any idea about this
To insert widget to the layout counting from the end. For example, I’d like to create the add button to add widget at the second position from the end as below.
To allow users to select one column from the QTreeWidget as below. (If you’d like to know how to generate tree widget, check “How to use QTreeWidget() in PySide2” first.)
Though the following is one of the ways to access items of multiple list, an error “index out of range” will occur when the lengths of the lists are different. How can I solve it?
list1 = ["A", "B", "C", "D"]
list2 = [1, 2, 3, 4]
list3 = ["Apple", "Orange", "Banana", "Peach"]
for i in range(len(list1)):
print(list1[i], list2[i], list3[i])
The output is as below.
A 1 Apple
B 2 Orange
C 3 Banana
D 4 Peach
GOAL
To access each n-th item of multiple lists in one for loop.