I am trying to create a tableWidget in PyQt that has copy-paste functionality and the ability to create new tabs. My table is loaded with a initialized sheet but gives the user the ability to create new tabs with new qTableWidgets which I handled using a for loop to create and initialize the widget everytime a new tab is created in my add_sheet() function.
I wanted to also add functionality for copying and pasting inside each tab and across the tabs. Now when I added the key press event function to do this, I kept getting errors when trying to copy and paste in new tabs as out of index. I tried to fix this by keeping a pointer of which tab the selected indexes come from but this only allows me to edit on the first new tab created. The initial spreadsheet crashes when trying to do operations and the other tabs just do not work. It also does not copy and paste universally amongst the tabs.
I feel I have made my handling too complicated and I have a flaw or am missing something in my design pattern.
How can I properly implement my copy-paste function to work amongst all dynamically tabs with their own QTableWidget instances including the initial QTableWidget created both locally and universally?
import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow,
QTableWidget, QTableWidgetItem,QVBoxLayout,QTabWidget,QWidget,QToolButton,QToolBar)
from PyQt6.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tab_widget = MyTabWidget(self)
self.setCentralWidget(self.tab_widget)
self.initializeUI()
def initializeUI(self):
"""Set up the application's GUI."""
self.setMinimumSize(1200, 500)
self.setWindowTitle("Spreadsheet - QTableWidget Example")
# Used for copy and paste actions
self.item_text = None
self.setUpMainWindow()
self.show()
def setUpMainWindow(self):
"""Create and arrange widgets in the main window."""
# Set initial row and column values
main_spreadsheet_widget.setRowCount(10)
main_spreadsheet_widget.setColumnCount(10)
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.sheets = QTabWidget()
self.main_sheet = QWidget()
self.sheets.resize(300, 200)
self.extra_sheets_tracker = list()
self.tab_index = []
self.copied_cells_list = []
self.paste_index = []
# Add sheets
self.sheets.addTab(self.main_sheet, "Main Sheet")
#self.sheets.addTab(self.tab3, "Geeks")
# Create first tab
self.main_sheet.layout = QVBoxLayout(self)
self.main_sheet.layout.addWidget(main_spreadsheet_widget)
self.main_sheet.setLayout(self.main_sheet.layout)
self.tabButton = QToolButton(self)
self.tabButton.setText('+')
font = self.tabButton.font()
font.setBold(True)
self.tabButton.setFont(font)
self.sheets.setCornerWidget(self.tabButton)
self.tabButton.clicked.connect(self.add_sheet)
# Add sheets to widget
self.layout.addWidget(self.sheets)
self.setLayout(self.layout)
def add_sheet(self):
self.sheet = QWidget()
self.main_tab_sheet_widget = QTableWidget()
self.extra_sheets_tracker.append(self.main_tab_sheet_widget)
self.main_tab_sheet_widget.setRowCount(10)
self.main_tab_sheet_widget.setColumnCount(10)
self.sheet.layout = QVBoxLayout(self)
self.sheet.layout.addWidget(self.main_tab_sheet_widget)
self.sheet.setLayout(self.sheet.layout)
self.sheets.addTab(self.main_tab_sheet_widget, "Sheet" + str(len(self.extra_sheets_tracker)))
def keyPressEvent(self, event):
super().keyPressEvent(event)
if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
self.tab_index = []
for i in self.extra_sheets_tracker:
if i.selectedIndexes() is not None:
self.tab_index.append(self.extra_sheets_tracker.index(i))
self.copied_cells = sorted(self.extra_sheets_tracker[self.tab_index[0]].selectedIndexes())
self.copied_cells_list.append(self.copied_cells)
self.copied_cells = None
elif event.key() == Qt.Key.Key_V and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
self.paste_index = []
for i in self.extra_sheets_tracker:
self.paste_index.append(self.extra_sheets_tracker.index(i))
r = self.extra_sheets_tracker[self.paste_index[0]].currentRow() - self.copied_cells_list[0][0].row()
c = self.extra_sheets_tracker[self.paste_index[0]].currentColumn() - self.copied_cells_list[0][0].column()
for cell in self.copied_cells_list[0]:
self.extra_sheets_tracker[self.paste_index[0]].setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))
if __name__ == "__main__":
app = QApplication(sys.argv)
main_spreadsheet_widget = QTableWidget()
window = MainWindow()
window.show()
sys.exit(app.exec())
I have managed to do the following:
1- I treated the main sheet as if it was "just another sheet" and made sure it follows the same relative routine. This included removing unnecessary functions like setUpMainWindow and introducing static values like MAX_ROWS and MAX_COLS for maximum rows and columns respectively. This also entailed renaming extra_sheets_tracker to become sheets_tracker since it will hold all of the sheets.
2- I have created two strategies for pasting: 1st-cell for selecting the first cell in a table, or 1-to-1 for selecting the exact count of cells at the pasting side. This meant, also, checking for the dimensions beforehand and raising the exception Unmatching pasting size if the sizes did not match.
3- I removed self.tab_index and self.copied_cells_list since they are not used. copy_list is used globally and it is not a bad idea to stay in self. However paste_list is not used anywhere else than the paste routine and I removed it from self. You need to look into your self variables, you seem to overuse them.
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QTabWidget, QWidget, QToolButton, QToolBar
from PyQt6.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tab_widget = MyTabWidget(self)
self.setCentralWidget(self.tab_widget)
self.initializeUI()
def initializeUI(self):
"""Set up the application's GUI."""
self.setMinimumSize(1200, 500)
self.setWindowTitle("Spreadsheet - QTableWidget Example")
# Used for copy and paste actions
self.item_text = None
self.show()
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.MAX_ROWS = 10
self.MAX_COLS = 10
self.STRATEGY = {0:'1st-cell', 1:'1-to-1'}
self.sheets = QTabWidget()
self.main_sheet = QWidget()
self.sheets.resize(300, 200)
self.sheets_tracker = list()
self.copy_list = []
#self.copied_cells_list = []
#self.paste_list = []
# Add a sheet and create first tab
self.main_sheet.layout = QVBoxLayout(self)
# Setup main window
self.add_sheet()
tabButton = QToolButton(self)
tabButton.setText('+')
font = tabButton.font()
font.setBold(True)
tabButton.setFont(font)
tabButton.clicked.connect(self.add_sheet)
self.sheets.setCornerWidget(tabButton)
# Add sheets to widget
self.layout.addWidget(self.sheets)
self.setLayout(self.layout)
def add_sheet(self):
sheets_count = len(self.sheets_tracker)
if sheets_count < 1:
label = "Main sheet"
else:
label = f"Sheet{sheets_count}"
self.main_tab_sheet_widget = QTableWidget()
self.main_tab_sheet_widget.setRowCount(self.MAX_ROWS)
self.main_tab_sheet_widget.setColumnCount(self.MAX_COLS)
self.sheets_tracker.append(self.main_tab_sheet_widget)
self.sheet = QWidget()
self.sheet.layout = QVBoxLayout(self)
self.sheet.layout.addWidget(self.main_tab_sheet_widget)
self.sheet.setLayout(self.sheet.layout)
self.sheets.addTab(self.main_tab_sheet_widget, label)
def keyPressEvent(self, event):
super().keyPressEvent(event)
if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
self.copy_list = sorted([(e.row(), e.column(), e.data()) for e in self.sheets_tracker[self.sheets.currentIndex()].selectedIndexes()])
if len(self.copy_list) != 0:
# recalculate the indicies to absolute values
least_row = self.copy_list[0][0]
least_col = sorted(self.copy_list, key=lambda tup: tup[1])[0][1]
self.copy_list = [(e[0]-least_row, e[1]-least_col, e[2]) for e in self.copy_list]
print(self.copy_list)
elif event.key() == Qt.Key.Key_V and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
paste_list = sorted([(element.row(), element.column(), element.data()) for element in self.sheets_tracker[self.sheets.currentIndex()].selectedIndexes()])
if len(paste_list) != 0:
if len(paste_list) == 1:
# The given paste position is the first cell only
current_r, current_c, _ = paste_list[-1]
last_r, last_c, _ = self.copy_list[-1]
if last_r + current_r - 1 < self.MAX_ROWS and last_c + current_c - 1 < self.MAX_COLS:
strategy = self.STRATEGY[0]
else:
raise Exception('Unmatching pasting size')
elif len(self.copy_list) == len(paste_list):
# You can paste in one-to-one correspondence
strategy = self.STRATEGY[1]
else:
raise Exception('Unmatching pasting size')
if strategy == self.STRATEGY[0]:
r, c, _ = paste_list[0]
for index, e in enumerate(self.copy_list):
_, _, d = self.copy_list[index]
d = '' if d is None else d
print(f"Pasting at index [{self.sheets.currentIndex()}] cell ({e[0]+r}, {e[1]+c})")
textCell = QTableWidgetItem(); textCell.setText(f'{d}')
self.sheets_tracker[self.sheets.currentIndex()].setItem(e[0]+r, e[1]+c, textCell)
if strategy == self.STRATEGY[1]:
for index, e in enumerate(paste_list):
_, _, d = self.copy_list[index]
d = '' if d is None else d
print(f"Pasting at index [{self.sheets.currentIndex()}] cell ({e[0]}, {e[1]})")
textCell = QTableWidgetItem(); textCell.setText(f'{d}')
self.sheets_tracker[self.sheets.currentIndex()].setItem(e[0], e[1], textCell)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
Related
Some context:
I made a QTreeView and set QFileSystemModel as its model. There are 2 TreeViews, the one on the right corresponds in the list of the project files in a directory, and the left one serves for the view of the connected files for that project file. With help, I've implemented this behavior where when I click on a project file on the right side, its connected files will appear on the left side. (I removed the mechanism of storing and the connected files as it will add a ton of code)
The problem:
After adding the 2 TreeViews above, I then added a context menu with only one option, which is to delete the selected files. I think my first attempt which is on the left side is successful because I can see the path printed in the console. Now the problem lies when I tried to do it on the right side. Knowing that the model in this side is subclassed from the QSortFilterProxyModel, I then added the sourceModel() so that it will return the QFileSystemModel. But when I tried to run the file, It just gives me an error. You can see my implementation below.
def deleteFile(self, event):
index_list = self.tree_sender.selectedIndexes()
for index in index_list:
if index.column() == 0:
if isinstance(self.tree_sender.model(), ModifiedQSortFilterProxyModel)
fileIn = self.tree_sender.model().sourceModel().fileInfo(index)
else:
fileIn = self.tree_sender.model().fileInfo(index)
# method above is also not working even I add sourceModel()
# It just ends the app imediately
path_to_delete = fileIn.absoluteFilePath()
print(path_to_delete)
My Testing Code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView, QFileSystemModel, QHBoxLayout, \
QVBoxLayout, QPushButton, QListWidget, QListWidgetItem, QMenu, QAction, QAbstractItemView
from PyQt5.QtCore import QSortFilterProxyModel, Qt
from PyQt5.QtGui import QCursor
class ModifiedQSortFilterProxyModel(QSortFilterProxyModel):
fileInfo = None
con_files = None
def filterAcceptsRow(self, source_row, source_parent):
if not self.fileInfo:
return True
source_index = self.sourceModel().index(source_row, 0, source_parent)
info = self.sourceModel().fileInfo(source_index)
if self.fileInfo.absolutePath() != info.absolutePath():
return True
return info.fileName() in self.con_files
def setFilter(self, info, connected_files):
self.fileInfo = info
self.con_files = connected_files
self.invalidateFilter()
class FileSystemView(QWidget):
def __init__(self):
super().__init__()
appWidth = 800
appHeight = 300
self.setWindowTitle('File System Viewer')
self.setGeometry(300, 300, appWidth, appHeight)
dir_path = r'<your directory>'
# -- left -- #
self.model = QFileSystemModel()
self.model.setRootPath(dir_path)
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setRootIndex(self.model.index(dir_path))
self.tree.setColumnWidth(0, 250)
self.tree.setAlternatingRowColors(True)
self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.tree.clicked.connect(self.onClicked)
self.tree.setContextMenuPolicy(Qt.CustomContextMenu)
self.tree.customContextMenuRequested.connect(self.context_menu)
# -- right -- #
self.model2 = QFileSystemModel()
self.model2.setRootPath(dir_path)
self.model2.setReadOnly(False)
self.filter_proxy_model = ModifiedQSortFilterProxyModel()
self.filter_proxy_model.setSourceModel(self.model2)
self.filter_proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.filter_proxy_model.setDynamicSortFilter(True)
self.filter_proxy_model.setFilterKeyColumn(0)
root_index = self.model2.index(dir_path)
proxy_index = self.filter_proxy_model.mapFromSource(root_index)
self.tree2 = QTreeView()
self.tree2.setModel(self.filter_proxy_model)
self.tree2.setRootIndex(proxy_index)
self.tree2.setColumnWidth(0, 250)
self.tree2.setAlternatingRowColors(True)
self.tree2.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.tree2.setContextMenuPolicy(Qt.CustomContextMenu)
self.tree2.customContextMenuRequested.connect(self.context_menu)
# -- layout -- #
self.tree_layout = QHBoxLayout()
self.tree_layout.addWidget(self.tree)
self.tree_layout.addWidget(self.tree2)
self.setLayout(self.tree_layout)
def context_menu(self, event):
self.tree_sender = self.sender()
# ^^^ I dont know how to access the treeview where context menu
# ^^^ is triggered so I just tried to pass it as a variable
self.menu = QMenu(self)
deleteAction = QAction('Delete', self)
deleteAction.triggered.connect(lambda event: self.deleteFile(event))
self.menu.addAction(deleteAction)
self.menu.popup(QCursor.pos())
def deleteFile(self, event):
index_list = self.tree_sender.selectedIndexes()
for index in index_list:
if index.column() == 0:
if isinstance(self.tree_sender.model(), ModifiedQSortFilterProxyModel):
fileIn = self.tree_sender.model().sourceModel().fileInfo(index)
else:
fileIn = self.tree_sender.model().fileInfo(index)
# method above is also not working even I add sourceModel()
# It just ends the app imediately
path_to_delete = fileIn.absoluteFilePath()
print(path_to_delete)
def onClicked(self, index):
print("onclick")
connected_files = [] # list of connected files
self.filter_proxy_model.setFilter(self.model.fileInfo(index), connected_files)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = FileSystemView()
demo.show()
sys.exit(app.exec_())
My Question: What causes the error? is it sourceModel() implementation? or I did something very wrong?
I hope someone can point me out to the right direction.
I totally forgot to use mapToSource, but thankfully #musicamante guided me through it. My question is now solved, and the final implementation of the deleteFile method is given below.
def deleteFile(self, event):
index_list = self.tree_sender.selectedIndexes()
for index in index_list:
if index.column() == 0:
model = self.tree_sender.model()
if isinstance(model, ModifiedQSortFilterProxyModel):
proxy_index = model.mapToSource(index)
fileIn = proxy_index.model().fileInfo(proxy_index)
else:
fileIn = model.fileInfo(index)
path_to_delete = fileIn.absoluteFilePath()
print(path_to_delete)
I want to use QtreeView to organize the data shown by a QComboBox. As you can see in my example, creating the box and setting up data works so far.
But my problem is, that the combobox itself only shows the first argument and not the whole line. what I want to have is, that there is shown the whole row, not only the first item of the row.
Is this maybe related to the fact, that each cell is selectable? Do I have to prohibit to select items at the end of the tree branch?
How can I achieve this while adding the elements to the QtreeView-data?
minimal example:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
data = [['a','b','c'],['d','e','f'],['g','h','i']]
class MainWindow(QMainWindow):
dispatcher = 0
def __init__(self):
super().__init__()
# buil UI
self.init_ui()
def init_ui(self):
# layout
self.box_window = QVBoxLayout()
# content
model = QStandardItemModel(len(data),len(data[0]))
row = 0
for r in data:
col = 0
for item in r:
model.setData(model.index(row, col), item)
col += 1
row += 1
tree_view = QTreeView()
tree_view.setHeaderHidden(True)
tree_view.setRootIsDecorated(False)
tree_view.setAlternatingRowColors(True)
combobox = QComboBox()
combobox.setMinimumSize(250,50)
combobox.setView(tree_view)
combobox.setModel(model)
self.box_window.addWidget(combobox)
self.box_window.addStretch()
# build central widget and select it
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.centralWidget().setLayout(self.box_window)
# show window
self.setGeometry(50,50,1024,768)
self.setWindowTitle("Test")
self.show()
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
A possible solution is to concatenate the texts in the row and set as the text to be painted:
class ComboBox(QComboBox):
def paintEvent(self, event):
painter = QStylePainter(self)
painter.setPen(self.palette().color(QPalette.Text))
# draw the combobox frame, focusrect and selected etc.
opt = QStyleOptionComboBox()
self.initStyleOption(opt)
values = []
for c in range(self.model().columnCount()):
index = self.model().index(self.currentIndex(), c, self.rootModelIndex())
values.append(index.data())
opt.currentText = " ".join(values)
painter.drawComplexControl(QStyle.CC_ComboBox, opt)
# draw the icon and text
painter.drawControl(QStyle.CE_ComboBoxLabel, opt)
Once an item is selected on a combo, it should get removed from the others, unless it's "No use".
I made three options of QComboBox, and each of those contains the same items.
The explanation is this:
QCombobox Tug 1 has total 4 items ('No Use', '207HR', '306DR', 'Jupiter')
QCombobox Tug 2 has total 4 items ('No Use', '207HR', '306DR', 'Jupiter')
QCombobox Tug 3 has total 4 items ('No Use', '207HR', '306DR', 'Jupiter')
The default value of those Qcombobox is 'No Use'.
How can I remove the selected value of QComboBox Tug 1 from QComboBox Tug 2?
The point is that 'No Use' shall not be removed; only an item from among '207HR', '306DR', and 'Jupiter'.
The Code i made is below:
class Ship_Use_Tug_Input_Program(QWidget):
def __init__(self, master):
super().__init__()
self.initUI()
def initUI(self):
tug1_cb = QComboBox(self)
jeju_tug = ['No use','207HR (2,500HP)', '306DR (3,600HP)', 'Jupiter (3,600HP)']
tug1_cb.addItems(jeju_tug)
tug2_cb = QComboBox(self)
tug2_cb.addItems(jeju_tug)
tug3_cb = QComboBox(self)
tug3_cb.addItems(jeju_tug)
self.setGeometry(100,100,1000,500)
self.setWindowTitle('Ship_Use_Tug_Input_Program')
self.show()
app = QApplication(sys.argv)
exc = Ship_Use_Tug_Input_Program(master=Ship_Use_Tug_Input_Program)
app.exec_()
The explanation photo is below:
The view-widget of the combo-box can be used to hide the rows, and the item-data can be used to keep track of which combo-box is showing which row. A slot connected to the activated signal can then update the items whenever one of the current-items change.
Below is a complete demo script that implements that. The ExclusiveComboGroup class can be used with any group of combo-boxes. To use it, just create an instance and then add all your combo-boxes using its addCombo method.
Demo Script:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class ExclusiveComboGroup(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._combos = []
self._role = Qt.UserRole + 500
def addCombo(self, combo):
combo.activated.connect(
lambda: self.handleActivated(combo))
self._combos.append(combo)
def handleActivated(self, target):
index = target.currentIndex()
groupid = id(target)
for combo in self._combos:
if combo is target:
continue
previous = combo.findData(groupid, self._role)
if previous >= 0:
combo.view().setRowHidden(previous, False)
combo.setItemData(previous, None, self._role)
if index > 0:
combo.setItemData(index, groupid, self._role)
combo.view().setRowHidden(index, True)
class Window(QWidget):
def __init__(self):
super().__init__()
self.group = QGroupBox('Selected Tug')
layout = QVBoxLayout(self)
layout.addWidget(self.group)
layout = QFormLayout(self.group)
layout.setVerticalSpacing(15)
layout.setHorizontalSpacing(50)
jeju_tug = [
'No use',
'207HR (2,500HP)',
'306DR (3,600HP)',
'Jupiter (3,600HP)',
]
# create a combo-group
self.tugs = ExclusiveComboGroup(self)
for index in range(3):
combo = QComboBox(self)
combo.addItems(jeju_tug)
layout.addRow(f'Tug {index + 1}', combo)
# add the combo-box
self.tugs.addCombo(combo)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.setWindowTitle('Demo')
window.setGeometry(800, 200, 100, 50)
window.show()
sys.exit(app.exec_())
PS: here is how to use it in your own example:
def initUI(self):
tug1_cb = QComboBox(self)
jeju_tug = ['No use','207HR (2,500HP)', '306DR (3,600HP)', 'Jupiter (3,600HP)']
tug1_cb.addItems(jeju_tug)
tug2_cb = QComboBox(self)
tug2_cb.addItems(jeju_tug)
tug3_cb = QComboBox(self)
tug3_cb.addItems(jeju_tug)
# copy the ExclusiveComboGroup class into
# your code and then add this section
tugs = ExclusiveComboGroup(self)
tugs.addCombo(tug1_cb)
tugs.addCombo(tug2_cb)
tugs.addCombo(tug3_cb)
I am implement my project using pyqt5. Currently, I have a window including many widget. Now, I want to remove some widgets. The window looks like:
Now, I want to remove the 'name1' widget including the QLabel and QPushButton.
However, after removing all 'name1' widgets, the 'name2' widgets including QLabel and QPushButton can not self-adapte with the window, like:
All my code is:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QDialog):
def __init__(self):
super().__init__()
self.initGUI()
self.show()
def initGUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
removeLayout = QHBoxLayout()
self.__removeText = QLineEdit()
self.__removeBtn = QPushButton('Remove')
self.__removeBtn.clicked.connect(self.remove)
removeLayout.addWidget(self.__removeText)
removeLayout.addWidget(self.__removeBtn)
ROIsLayout = QVBoxLayout()
for name in ['name1', 'name2']:
subLayout = QHBoxLayout()
subText = QLabel(name)
subText.setObjectName(name)
subBtn = QPushButton(name)
subBtn.setObjectName(name)
subLayout.addWidget(subText)
subLayout.addWidget(subBtn)
ROIsLayout.addLayout(subLayout)
layout.addLayout(removeLayout)
layout.addLayout(ROIsLayout)
self.__ROIsLayout = ROIsLayout
def remove(self, checked=False):
name = self.__removeText.text()
while True:
child = self.__ROIsLayout.takeAt(0)
if child == None:
break
while True:
subChild = child.takeAt(0)
if subChild == None:
break
obName = subChild.widget().objectName()
if name == obName:
widget = subChild.widget()
widget.setParent(None)
child.removeWidget(widget)
self.__ROIsLayout.removeWidget(widget)
del widget
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
update:
Actually, the issue may be the takeAt. The following code is workable:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QDialog):
def __init__(self):
super().__init__()
self.initGUI()
self.show()
def initGUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
removeLayout = QHBoxLayout()
self.__removeText = QLineEdit()
self.__removeBtn = QPushButton('Remove')
self.__removeBtn.clicked.connect(self.remove)
removeLayout.addWidget(self.__removeText)
removeLayout.addWidget(self.__removeBtn)
ROIsLayout = QVBoxLayout()
for name in ['name1', 'name2']:
subLayout = QHBoxLayout()
subLayout.setObjectName(name)
subText = QLabel(name, parent=self)
subText.setObjectName(name)
subBtn = QPushButton(name, parent=self)
subBtn.setObjectName(name)
subLayout.addWidget(subText)
subLayout.addWidget(subBtn)
ROIsLayout.addLayout(subLayout)
print(name, subLayout, subText, subBtn)
layout.addLayout(removeLayout)
layout.addLayout(ROIsLayout)
self.__ROIsLayout = ROIsLayout
self.record = [subLayout, subText, subBtn]
def remove(self, checked=False):
layout = self.record[0]
txt = self.record[1]
btn = self.record[2]
layout.removeWidget(txt)
txt.setParent(None)
txt.deleteLater()
layout.removeWidget(btn)
btn.setParent(None)
btn.deleteLater()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
But, I have printed the QLabel/QPushButton in the self.record, and I find it is the same with that from child.takeAt(0).widget().
The main issue in your code is that you're constantly using takeAt(). The result is that all items in the __ROIsLayout layout will be removed from it (but not deleted), which, in your case, are the sub layouts. This is clearly not a good approach: only the widgets with the corresponding object name will be actually deleted, while the others will still be "owned" by their previous parent, will still be visible at their previous position and their geometries won't be updated since they're not managed by the layout anymore.
There are multiple solutions to your question, all depending on your needs.
If you need to remove rows from a layout, I'd consider setting the object name on the layout instead, and look for it using self.findChild().
Also consider that, while Qt allows setting the same object name for more than one object, that's not suggested.
Finally, while using del is normally enough, it's usually better to call deleteLater() for all Qt objects, which ensures that Qt correctly removes all objects (and related parentship/connections).
Another possibility, for this specific case, is to use a QFormLayout.
I am really new to pyqt and this question maybe silly. Any help, I would really appreciate. I have this code I get from this page where it codes in filtering a qtablewidget. This code works perfectly fine to my desired filtering output. However I have a table and it has lot of rows, I want the menu bar used in filtering to be scrollable instead of displaying all unique contents in the row. I want to have fixed size height of the menubar.
This is the code:
import csv
import sys
from PyQt5 import QtCore
from PyQt5 import QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.verticalLayout = QtWidgets.QVBoxLayout(self)
self.table = QtWidgets.QTableWidget(self)
self.table.setColumnCount(0)
self.table.setRowCount(0)
self.verticalLayout.addWidget(self.table)
self.loadAll()
self.horizontalHeader = self.table.horizontalHeader()
self.horizontalHeader.sectionClicked.connect(self.on_view_horizontalHeader_sectionClicked)
self.keywords = dict([(i, []) for i in range(self.table.columnCount())])
self.checkBoxs = []
self.col = None
def slotSelect(self, state):
for checkbox in self.checkBoxs:
checkbox.setChecked(QtCore.Qt.Checked == state)
def on_view_horizontalHeader_sectionClicked(self, index):
self.menu = QtWidgets.QMenu()
self.col = index
data_unique = []
self.checkBoxs = []
checkBox = QtWidgets.QCheckBox("Select all", self.menu)
checkableAction = QtWidgets.QWidgetAction(self.menu)
checkableAction.setDefaultWidget(checkBox)
self.menu.addAction(checkableAction)
checkBox.setChecked(True)
checkBox.stateChanged.connect(self.slotSelect)
for i in range(self.table.rowCount()):
if not self.table.isRowHidden(i):
item = self.table.item(i, index)
if item.text() not in data_unique:
data_unique.append(item.text())
checkBox = QtWidgets.QCheckBox(item.text(), self.menu)
checkBox.setChecked(True)
checkableAction = QtWidgets.QWidgetAction(self.menu)
checkableAction.setDefaultWidget(checkBox)
self.menu.addAction(checkableAction)
self.checkBoxs.append(checkBox)
btn = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal, self.menu)
btn.accepted.connect(self.menuClose)
btn.rejected.connect(self.menu.close)
checkableAction = QtWidgets.QWidgetAction(self.menu)
checkableAction.setDefaultWidget(btn)
self.menu.addAction(checkableAction)
headerPos = self.table.mapToGlobal(self.horizontalHeader.pos())
posY = headerPos.y() + self.horizontalHeader.height()
posX = headerPos.x() + self.horizontalHeader.sectionPosition(index)
self.menu.exec_(QtCore.QPoint(posX, posY))
def menuClose(self):
self.keywords[self.col] = []
for element in self.checkBoxs:
if element.isChecked():
self.keywords[self.col].append(element.text())
self.filterdata()
self.menu.close()
def loadAll(self):
with open("pokemon_data.csv", "r") as inpfil:
reader = csv.reader(inpfil, delimiter=',')
csheader = next(reader)
ncol = len(csheader)
data = list(reader)
row_count = len(data)
self.table.setRowCount(row_count)
self.table.setColumnCount(ncol)
self.table.setHorizontalHeaderLabels(('%s' % ', '.join(map(str, csheader))).split(","))
for ii in range(0, row_count):
mainins = data[ii]
for var in range(0, ncol):
self.table.setItem(ii, var, QtWidgets.QTableWidgetItem(mainins[var]))
def clearFilter(self):
for i in range(self.table.rowCount()):
self.table.setRowHidden(i, False)
def filterdata(self):
columnsShow = dict([(i, True) for i in range(self.table.rowCount())])
for i in range(self.table.rowCount()):
for j in range(self.table.columnCount()):
item = self.table.item(i, j)
if self.keywords[j]:
if item.text() not in self.keywords[j]:
columnsShow[i] = False
for key, value in columnsShow.items():
self.table.setRowHidden(key, not value)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
This is currently what it looks like:
When i try to filter by clicking the header, all my desktop window filled up because I have so much rows in my table as shown in the image below.
i just need a better size of the menubar.
I tried searching related queries to this but I just couldn't find that I can integrate it to this code. Please be gentle with your response as Im really new to this. Thank you so much!
The easiest solution is to use an undocumented stylesheet property (as proposed in the unaccepted answer of this post).
def on_view_horizontalHeader_sectionClicked(self, index):
self.menu = QtWidgets.QMenu()
self.menu.setStyleSheet('QMenu { menu-scrollable: true; }')
# ...
Alternatively (if, for any reason, that behavior doesn't work as expected or its support is removed in the future) you can create a QProxyStyle subclass, implement its styleHint and return True if the given hint is SH_Menu_Scrollable.
class ProxyStyle(QtWidgets.QProxyStyle):
def styleHint(self, hint, option, widget, data):
if hint == self.SH_Menu_Scrollable:
return True
return super().styleHint(hint, option, widget, data)
# ...
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyle(ProxyStyle())
w = Widget()
w.show()
sys.exit(app.exec_())
If you want to keep that behavior only for that menu, you can apply the proxy style to the menu instead of doing it for the whole application:
def on_view_horizontalHeader_sectionClicked(self, index):
self.menu = QtWidgets.QMenu()
self.menu.setStyle(ProxyStyle())
# ...
By the way, you are using a menu, not a menubar. A menubar is the widget normally placed on the top of a window, containing different items, each one of it being (possibly) a menu.
UPDATE
Since the items are a lot, using a QMenu is not a good solution for various reasons.
A better approach would be to use a QWidget that contains a QListWidget and the button box. To keep the behavior similar to that of a menu (it should close if a click happens outside it), you can add the Popup window flag.
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
# ...
self.dialog = QtWidgets.QWidget()
self.dialog.setWindowFlags(
self.dialog.windowFlags() | QtCore.Qt.Popup | QtCore.Qt.FramelessWindowHint)
self.dialog.setMaximumHeight(300)
layout = QtWidgets.QVBoxLayout(self.dialog)
self.dialogList = QtWidgets.QListWidget()
layout.addWidget(self.dialogList)
self.dialogList.itemChanged.connect(self.slotSelect)
buttonBox = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
layout.addWidget(buttonBox)
buttonBox.accepted.connect(self.menuClose)
buttonBox.rejected.connect(self.dialog.hide)
def on_view_horizontalHeader_sectionClicked(self, index):
self.dialogList.clear()
self.selectAllItem = QtWidgets.QListWidgetItem('Select all')
self.selectAllItem.setCheckState(QtCore.Qt.Unchecked)
self.dialogList.addItem(self.selectAllItem)
self.col = index
self.itemList = []
data_unique = []
for i in range(self.table.rowCount()):
if not self.table.isRowHidden(i):
item = self.table.item(i, index)
if item == self.selectAllItem:
continue
if item.text() not in data_unique:
item = QtWidgets.QListWidgetItem(item.text())
item.setCheckState(QtCore.Qt.Unchecked)
self.dialogList.addItem(item)
self.itemList.append(item)
self.dialog.move(QtGui.QCursor.pos())
self.dialog.show()
def slotSelect(self, item):
# temporally disconnect the signal to avoid recursion
self.dialogList.itemChanged.disconnect(self.slotSelect)
if item == self.selectAllItem:
state = item.checkState()
for i in self.itemList:
i.setCheckState(state)
else:
states = [i.checkState() for i in self.itemList]
if all(states):
self.selectAllItem.setCheckState(QtCore.Qt.Checked)
elif not any(states):
self.selectAllItem.setCheckState(QtCore.Qt.Unchecked)
else:
self.selectAllItem.setCheckState(QtCore.Qt.PartiallyChecked)
# reconnect the signal back again
self.dialogList.itemChanged.connect(self.slotSelect)
def menuClose(self):
self.dialog.hide()
self.keywords[self.col] = []
for item in self.itemList:
if item.checkState():
self.keywords[self.col].append(item.text())
self.filterdata()