QTreeWidget - re-calculate row height per row content? - python

It's a quick one. I put a QWidget with layout containing 10 buttons in each of my QTreeWidgetItem that i can expand-contract.
If i contract the QWidget(hide), the row then remains the same height. It should get resized to be a lot smaller to contain only unhide button. The space occupied by the row remains empty. Only when I add a new widget. The row's height gets re-calculated. Same goes for expanding. If I expand widget, it still is 17 pixels in height. Only when I add a new widget the row's height gets recalculated.
Is there a built-in function in QTreeWidget that I can call to update its row height - content, or do I need to loop over each item and adjust its height manually?
Also is it a good idea to put a QWidget containing layout and lots of widgets inside QTreeWidgetItem? If I want to have lets say 500 QtreeWidgetItems, each having QTreeWidget with 20 Buttons/etc items in it?
Regards
Dariusz
Edit1.
I spend quite a bit of time on it. Tried different paths but so far no luck. I have attached my rough code to show the issue. Just click on hide 1-2 times to see what happens. I'm struggling to understand how the "sizeHint" thini work...
Attached my test below.
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
sys._excepthook = sys.excepthook
def my_exception_hook(exctype, value, traceback):
# print(exctype, value, traceback)
sys._excepthook(exctype, value, traceback)
sys.exit(1)
sys.excepthook = my_exception_hook
class myWidget(QWidget):
def __init__(self, parent, something):
super(QWidget, self).__init__()
self.MW = parent
self.MWa = something
self.lay_main = QGridLayout()
self.setLayout(self.lay_main)
self.le_lineA = QLineEdit()
self.btn_a = QPushButton("Hey")
self.btn_a.clicked.connect(self.hideunhide)
self.subWidget = QWidget()
self.subLay = QGridLayout()
self.subWidget.setLayout(self.subLay)
self.btn_b = QPushButton("Boo")
self.lay_main.addWidget(self.btn_a)
self.lay_main.addWidget(self.subWidget)
self.subLay.addWidget(self.le_lineA)
self.subLay.addWidget(self.btn_b)
print("Im created :- )")
self.size = False
def hideunhide(self):
if self.subWidget.isHidden():
self.subWidget.show()
self.size = True
self.MW.sizeHint(1)
self.MW.setSizeHint(0, QSize(0, 0))
else:
self.subWidget.hide()
self.size = False
self.MW.sizeHint(1)
self.MW.setSizeHint(0, QSize(0, 0))
class widgetItemDictUpdate(QTreeWidgetItem):
def __init__(self, parent, MW, title, dicId):
super(QTreeWidgetItem, self).__init__(parent)
self.MW = MW
self.dicId = dicId
class myDelegate(QItemDelegate):
def __init__(self, mw):
self.MW = mw
super(QItemDelegate, self).__init__()
def sizeHint(self, option, index):
print("myDelegate", option, index)
if self.MW.itemFromIndex(index).layer.size:
return QSize(100, 100)
return QSize(100, 30)
class treeWidget(QTreeWidget):
def __init__(self, MW):
QTreeWidget.__init__(self)
self.setUniformRowHeights(False)
self.MW = MW
self.itemClicked.connect(self.printClick)
self.setHeaderItem(QTreeWidgetItem(["Name", "ProgressBar", "extra", "", "", "", "", "", "", "", ""]))
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.dragItems = []
self.lastHidenItemsArray = []
self.hiddenItemsSet = []
self.delg = myDelegate(self)
self.setItemDelegate(self.delg)
self.addItems(self.invisibleRootItem())
def printClick(self, item, column):
print("Clicked")
def addItems(self, parent):
testParent = self.addSlave(parent, 'RandomA', 50)
testParent = self.addSlave(parent, 'RandomB', 60)
testParent = self.addSlave(parent, 'RandomBX', 60)
testParent = self.addSlave(parent, 'RandomBcvas', 60)
def addSlave(self, parent, title, id, expanded=True, visible=True):
item = widgetItemDictUpdate(parent, self, title, id)
myLayer = myWidget(item, self)
item.setText(0, title)
item.layer = myLayer
self.setItemWidget(item, 1, myLayer)
item.id = id
item.type = 1
return item
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.treeWidget = treeWidget(self)
layout = QVBoxLayout()
layout.addWidget(self.treeWidget)
self.setLayout(layout)
self.setGeometry(1000, 500, 500, 500)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

As far as I am aware there is no way to resize rows directly though you can achieve a dynamic row height by implementing your table as a QTreeView + QSortFilterProxyModel subclass pair (PyQt: How Can I set row heights of QTreeView)
Alternatively I imagine removing the widget from the table (removeItemWidget) and then instantly re-adding it would cause a row size recalculation though I have not tested that.
As for my opinions regarding this setup of layouts inside tables, it is worth noting two things:
Qt layouts are slow to populate and resize.
Qt tables with variable row heights are slow in general.
That being said 500 isn't a huge number. I would probably knock together a quick prototype and see how it performs on a range of machines to see if the performance is acceptable and then worry about exploring other routes if it is too slow. I expect performance in this case will be most dependent on how complex the layouts are.

Related

PyQt6 deleting custom widget with nested class causes the program to crash

I am using Python 3.9.5.
I have encountered some serious problem in my project and here is a minimum reproducible example code, along with some descriptions.
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
class Editor(QTextEdit):
doubleClicked = pyqtSignal(QTextEdit)
def __init__(self):
super().__init__()
self.setReadOnly(True)
def mouseDoubleClickEvent(self, e: QMouseEvent) -> None:
self.doubleClicked.emit(self)
class textcell(QGroupBox):
def __init__(self, text):
super().__init__()
self.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
self.label = QLabel(text)
self.apply = makebutton('Apply')
self.apply.hide()
self.editor = Editor()
self.editor.doubleClicked.connect(lambda: self.editor.setReadOnly(False))
self.editor.doubleClicked.connect(self.apply.show)
self.hbox = QHBoxLayout()
self.hbox.addSpacerItem(spacer)
self.hbox.addWidget(self.apply)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.label)
self.vbox.addWidget(self.editor)
self.vbox.addLayout(self.hbox)
self.setLayout(self.vbox)
self.apply.clicked.connect(self.on_ApplyClick)
def on_ApplyClick(self):
self.editor.setReadOnly(True)
self.apply.hide()
def makebutton(text):
button = QPushButton()
button.setFixedSize(60, 20)
button.setText(text)
return button
class songpage(QGroupBox):
def __init__(self, texts):
super().__init__()
self.init(texts)
self.setCheckable(True)
self.setChecked(False)
def init(self, texts):
self.vbox = QVBoxLayout()
artist = textcell('Artist')
artist.editor.setText(texts[0])
album = textcell('Album')
album.editor.setText(texts[1])
title = textcell('Title')
title.editor.setText(texts[2])
self.vbox.addWidget(artist)
self.vbox.addWidget(album)
self.vbox.addWidget(title)
self.setLayout(self.vbox)
spacer = QSpacerItem(0, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(405, 720)
self.setWindowTitle('example')
frame = self.frameGeometry()
center = self.screen().availableGeometry().center()
frame.moveCenter(center)
self.move(frame.topLeft())
self.centralwidget = QWidget(self)
vbox = QVBoxLayout(self.centralwidget)
hbox = QHBoxLayout()
add = makebutton('Add')
delete = makebutton('Delete')
hbox.addWidget(add)
hbox.addSpacerItem(spacer)
hbox.addWidget(delete)
vbox.addLayout(hbox)
self.scrollArea = QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
vbox.addWidget(self.scrollArea)
self.setCentralWidget(self.centralwidget)
add.clicked.connect(self.addObj)
delete.clicked.connect(self.deleteObj)
def addObj(self):
Obj = songpage(('AAA', 'BBB', 'CCC'))
self.verticalLayout.addWidget(Obj)
def deleteObj(self):
item = self.verticalLayout.itemAt(0)
widget = item.widget()
self.verticalLayout.removeItem(item)
self.verticalLayout.removeWidget(widget)
app = QApplication([])
window = Ui_MainWindow()
window.show()
app.exec()
The problem is very simple, if I click add button, the widget will be added and everything works fine, if I double click on a QTextEdit, its apply button will show and it will change from read only to editable.
After I click an apply button, the button will hide and the corresponding QTextEdit will be read only again.
I have finally managed to add a doubleClicked signal to QTextEdit.
And, the problem, is if I click delete button, instead of deleting the thing as expected, it crashes the whole application.
I am sorry the minimum reproducible example is so long, but I have only managed to reproduce the issue with all the code and I really don't know what went wrong.
So how to fix it?
Well, I have figured it out, it was caused by the spacer item that I add to every one of horizontal layouts where fixed-sized buttons are used.
All such layouts are using the same spacer item, exactly the same spacer item, not just identical.
From what I have observed, the spacer items are all references to the same object, they are all echos of the same object which lives at a fixed memory address.
I honestly don't understand how such thing works, that the same object can not only be added to multiple layouts and present in all the layouts concurrently, but also be added to the same layout multiple times, yet it always remains the original object, not duplicates of itself.
I thought when I added the same spacer item to multiple layouts, I didn't add the original spacer item, instead duplicates of the original item which are identical but are at different memory addresses, and clearly that isn't how Python works.
So when I remove a widget, everything inside it, everything inside its layout is deleted, and the spacer item is deleted.
Because all the spacer items in all the layouts are references to the original spacer item, when I delete one of the layouts, the original spacer item is deleted as well, and the spacer item is deleted from all other layouts, yet its shadows remain, and the item isn't properly removed from all the other layouts, the layouts contain references to an object that no longer exists, thus the application crashed.
By removing the definition of the spacer item and replacing adding the spacer item with .addStretch(), the bug is fixed.

How to save selected element from combobox as variable in PyQt5?

I'm interested in how to save a selected value from my combobox as variable, so when I press e.g. B then I want it to be saved as SelectedValueCBox = selected value, which would be B in this case.
Thank you for your help
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = "PyQt5 - StockWindow"
self.left = 0
self.top = 0
self.width = 200
self.height = 300
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.tab_widget = MyTabWidget(self)
self.setCentralWidget(self.tab_widget)
self.show()
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
#self.layout = QGridLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tabs.resize(300, 200)
self.tabs.addTab(self.tab1, "Stock-Picker")
self.tab1.layout = QGridLayout(self)
button = QToolButton()
self.tab1.layout.addWidget(button, 1,1,1,1)
d = {'AEX':['A','B','C'], 'ATX':['D','E','F'], 'BEL20':['G','H','I'], 'BIST100':['J','K','L']}
def callback_factory(k, v):
return lambda: button.setText('{0}_{1}'.format(k, v))
menu = QMenu()
self.tab1.layout.addWidget(menu, 1,1,1,1)
for k, vals in d.items():
sub_menu = menu.addMenu(k)
for v in vals:
action = sub_menu.addAction(str(v))
action.triggered.connect(callback_factory(k, v))
button.setMenu(menu)
self.tab1.setLayout(self.tab1.layout)
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Since you're already returning a lambda for the connection, the solution is to use a function instead.
class MyTabWidget(QWidget):
def __init__(self, parent):
# ...
def callback_factory(k, v):
def func():
self.selectedValueCBox = v
button.setText('{0}_{1}'.format(k, v))
return func
# ...
self.selectedValueCBox = None
Note that your code also has many issues.
First of all, you should not add the menu to the layout: not only it doesn't make any sense (the menu should pop up, while adding it to a layout makes it "embed" into the widget, and that's not good), but it also creates graphical issues especially because you added the menu to the same grid "slot" (1, 1, 1, 1) which is already occupied by the button.
Creating a layout with a widget as argument automatically sets the layout to that widget. While in your case it doesn't create a big issue (since you've already set the layout) you should not create self.tab1.layout with self. Also, since you've already set the QVBoxLayout (due to the parent argument), there's no need to call setLayout() again.
A widget container makes sense if you're actually going to add more than one widget. You're only adding a QTabWidget to its layout, so it's almost useless, and you should just subclass from QTabWidget instead.
Calling resize on a widget that is going to be added on a layout is useless, as the layout will take care of the resizing and the previous resize call will be completely ignored. resize() makes only sense for top level widgets (windows) or the rare case of widgets not managed by a layout.
self.layout() is an existing property of all QWidgets, you should not overwrite it. The same with self.width() and self.height() you used in the App class.
App should refer to an application class, but you're using it for a QMainWindow. They are radically different types of classes.
Finally, you have no combobox in your code. A combobox is widget that is completely different from a drop down menu like the one you're using. I suggest you to be more careful with the terminology in the future, otherwise your question would result very confusing, preventing people to actually being able to help you.

QTableWidget loads very slow after clearing it with QLineEdits inside

I have a QTableWidget with QLineEdits as CellWidgets inside of it.
If I clear the table and refill it with the same function it needs much longer to finish.
In this example the difference is between 0.1 and 3.9 seconds, but in my real code the difference is between 0.1 seconds and 10 minutes.
So here is the example code:
class Test_Layout(QWidget):
def __init__(self, parent=None):
super(Test_Layout, self).__init__(parent=None)
self.left = 0
self.top = 0
self.width = 0
self.height = 0
self.initUI()
self.isMaximized()
def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
start_time = time.time()
self.fillTable()
print(time.time() - start_time)
self.combo = QComboBox(self)
self.combo.addItem("Reset")
for i in range(0, 5):
self.combo.addItem(str(i))
self.combo.currentTextChanged.connect(self.on_combo_changed)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.combo)
self.vbox.addWidget(self.table)
self.setLayout(self.vbox)
def fill_row(self, row):
self.table.insertRow(row)
placeholder = QLineEdit()
self.table.setCellWidget(row, 0, placeholder)
placeholder = QLineEdit()
self.table.setCellWidget(row, 1, placeholder)
placeholder = QLineEdit()
self.table.setCellWidget(row, 2, placeholder)
def on_combo_changed(self, currentText):
self.table.setRowCount(0)
if currentText == "Reset":
start_time = time.time()
self.fillTable()
print(time.time() - start_time)
else:
for row in range(0, int(currentText)):
self.fill_row(row)
def createTable(self):
self.table = QTableWidget()
self.table.setColumnCount(3)
self.table.setHorizontalHeaderLabels([
"LineEdit0",
"LineEdit1",
"LineEdit2",
])
header = self.table.horizontalHeader()
for i in range(0, 3):
header.setSectionResizeMode(i, QHeaderView.ResizeToContents)
def fillTable(self):
for row in range(0, 1000):
self.fill_row(row)
Output:
0.14005303382873535
And after using the QCombobox and setting it back to "Reset":
3.9842889308929443
And before somebody asks, I fill the QTableWidget with QLineEdits, because I want to use placeholders.
The difference is not only due to the fact that you're using cell widgets for each cell (and, let me say that 3000 widgets are a lot), but because you're calling setRowCount() each time.
You can also see that the problem happens not after "clearing", but just when creating the new cells: just remove the first call to fillTable in the __init__ and the same delay occurs.
Each time the model layout changes (by adding/removing rows or columns), lots of things happen not only for the model, but for the view that shows its contents, and since you are adding rows individually, this results in longer time required for the view to process its contents, even if you cannot see it instantly (and that's because the repainting is queued and only happens as soon as the event queue is cleared).
To improve performance, in your case, you should call setRowCount() only once with the final number of rows that are going to be shown:
def fill_row(self, row):
# comment or remove the following line
# self.table.insertRow(row)
placeholder = QLineEdit()
self.table.setCellWidget(row, 0, placeholder)
placeholder = QLineEdit()
self.table.setCellWidget(row, 1, placeholder)
placeholder = QLineEdit()
self.table.setCellWidget(row, 2, placeholder)
def on_combo_changed(self, currentText):
self.table.setRowCount(0)
if currentText == "Reset":
start_time = time.time()
self.fillTable()
print(time.time() - start_time)
else:
count = int(currentText)
self.table.setRowCount(count)
for row in range(0, int(currentText)):
self.fill_row(row)
def fillTable(self):
self.table.setRowCount(1000)
for row in range(0, 1000):
self.fill_row(row)
Finally, if you're really going to show that many rows, I strongly suggest to find an alternative, as the documentation explains for setIndexWidget() (which is internally called by setCellWidget()):
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.
This is because large amounts of widgets will cause drastic performances issues (exactly like yours).
If what you need is a placeholder, using a QLineEdit for each cell is a bad choice, not only for performance reasons, but also because in that way you don't have direct access to the model data, but you would always need to find the cell widget before that.
A more elegant and preferable solution is to use a custom delegate, which will show the placeholder text when there is no data for the cell, and add CurrentChanged to the table edit triggers:
self.table.setEditTriggers(self.table.editTriggers() | self.table.CurrentChanged)
A simple delegate implementation could be like this:
class PlaceholderDelegate(QStyledItemDelegate):
def __init__(self, placeholder='', parent=None):
super().__init__(parent)
self.placeholder = placeholder
def createEditor(self, parent, option, index):
editor = super().createEditor(parent, option, index)
if isinstance(editor, QLineEdit):
editor.setPlaceholderText(self.placeholder)
return editor
def paint(self, painter, option, index):
super().paint(painter, option, index)
if not index.data():
try:
# placeholderText palette role was introduced on Qt 5.12
color = option.palette.placeholderText().color()
except:
color = option.palette.text().color()
color.setAlpha(128)
painter.setPen(color)
style = option.widget.style()
margin = style.pixelMetric(style.PM_FocusFrameHMargin, option, option.widget)
rect = option.rect.adjusted(margin, 0, -margin, 0)
text = option.fontMetrics.elidedText(self.placeholder, option.textElideMode, rect.width())
painter.drawText(rect, option.displayAlignment, text)
class Test_Layout(QWidget):
# ...
def createTable(self):
# ...
for i in range(0, 3):
header.setSectionResizeMode(i, QHeaderView.ResizeToContents)
self.table.setItemDelegateForColumn(
i, PlaceholderDelegate('empty {}'.format(i + 1), self.table))
Note: you should not use setGeometry() with 0 width and height, and always provide a default position can be very annoying for users with large screens or more than one screen; also, width and height are default properties for all QWidget subclasses, and should never be overwritten with custom attributes.

Force QTabBar tabs to stay as small as possible and ignore sizeHint

I'm trying to have a + button added to a QTabBar. There's a great solution from years ago, with a slight issue that it doesn't work with PySide2. The problem is caused by the tabs auto resizing to fill the sizeHint, which in this case isn't wanted as the extra space is needed. Is there a way I can disable this behaviour?
I've tried QTabBar.setExpanding(False), but according to this answer, the property is mostly ignored:
The bad news is that QTabWidget effectively ignores that property, because it always forces its tabs to be the minimum size (even if you set your own tab-bar).
The difference being in PySide2, it forces the tabs to be the preferred size, where I'd like the old behaviour of minimum size.
Edit: Example with minimal code. The sizeHint width stretches the tab across the full width, whereas in older Qt versions it doesn't do that. I can't really override tabSizeHint since I don't know what the original tab size should be.
import sys
from PySide2 import QtCore, QtWidgets
class TabBar(QtWidgets.QTabBar):
def sizeHint(self):
return QtCore.QSize(100000, super().sizeHint().height())
class Test(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
tabWidget = QtWidgets.QTabWidget()
tabWidget.setTabBar(TabBar())
layout.addWidget(tabWidget)
tabWidget.addTab(QtWidgets.QWidget(), 'this shouldnt be stretched')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = Test()
test.show()
sys.exit(app.exec_())
I think there may be an easy solution to your problem (see below). Where the linked partial solution calculated absolute positioning for the '+' button, the real intent with Qt is always to let the layout engine do it's thing rather than trying to tell it specific sizes and positions. QTabWidget is basically a pre-built amalgamation of layouts and widgets, and sometimes you just have to skip the pre-built and build your own.
example of building a custom TabWidget with extra things across the TabBar:
import sys
from PySide2 import QtWidgets
from random import randint
class TabWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
#layout for entire widget
vbox = QtWidgets.QVBoxLayout(self)
#top bar:
hbox = QtWidgets.QHBoxLayout()
vbox.addLayout(hbox)
self.tab_bar = QtWidgets.QTabBar()
self.tab_bar.setMovable(True)
hbox.addWidget(self.tab_bar)
spacer = QtWidgets.QSpacerItem(0,0,QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
hbox.addSpacerItem(spacer)
add_tab = QtWidgets.QPushButton('+')
hbox.addWidget(add_tab)
#tab content area:
self.widget_stack = QtWidgets.QStackedLayout()
vbox.addLayout(self.widget_stack)
self.widgets = {}
#connect events
add_tab.clicked.connect(self.add_tab)
self.tab_bar.currentChanged.connect(self.currentChanged)
def add_tab(self):
tab_text = 'tab' + str(randint(0,100))
tab_index = self.tab_bar.addTab(tab_text)
widget = QtWidgets.QLabel(tab_text)
self.tab_bar.setTabData(tab_index, widget)
self.widget_stack.addWidget(widget)
self.tab_bar.setCurrentIndex(tab_index)
def currentChanged(self, i):
if i >= 0:
self.widget_stack.setCurrentWidget(self.tab_bar.tabData(i))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = TabWidget()
test.show()
sys.exit(app.exec_())
All that said, I think the pre-built QTabWidget.setCornerWidget may be exactly what you're looking for (set a QPushButton to the upper-right widget). The example I wrote should much easier to customize, but also much more effort to re-implement all the same functionality. You will have to re-implement some of the signal logic to create / delete / select / rearrange tabs on your own. I only demonstrated simple implementation, which probably isn't bulletproof to all situations.
Using the code from Aaron as a base to start on, I managed to implement all the functionality required to work with my existing script:
from PySide2 import QtCore, QtWidgets
class TabBar(QtWidgets.QTabBar):
def minimumSizeHint(self):
"""Allow the tab bar to shrink as much as needed."""
minimumSizeHint = super(TabBar, self).minimumSizeHint()
return QtCore.QSize(0, minimumSizeHint.height())
class TabWidgetPlus(QtWidgets.QWidget):
tabOpenRequested = QtCore.Signal()
tabCountChanged = QtCore.Signal(int)
def __init__(self, parent=None):
self._addingTab = False
super(TabWidgetPlus, self).__init__(parent=parent)
# Main layout
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
# Bar layout
self._tabBarLayout = QtWidgets.QHBoxLayout()
self._tabBarLayout.setContentsMargins(0, 0, 0, 0)
self._tabBarLayout.setSpacing(0)
layout.addLayout(self._tabBarLayout)
self._tabBar = TabBar()
self._tabBarLayout.addWidget(self._tabBar)
for method in (
'isMovable', 'setMovable',
'tabsClosable', 'setTabsClosable',
'tabIcon', 'setTabIcon',
'tabText', 'setTabText',
'currentIndex', 'setCurrentIndex',
'currentChanged', 'tabCloseRequested',
):
setattr(self, method, getattr(self._tabBar, method))
self._plusButton = QtWidgets.QPushButton('+')
self._tabBarLayout.addWidget(self._plusButton) # TODO: Find location to insert
self._plusButton.setFixedWidth(20)
self._tabBarLayout.addStretch()
# Content area
self._contentArea = QtWidgets.QStackedLayout()
layout.addLayout(self._contentArea)
# Signals
self.currentChanged.connect(self._currentChanged)
self._plusButton.clicked.connect(self.tabOpenRequested.emit)
# Final setup
self.installEventFilter(self)
#QtCore.Slot(int)
def _currentChanged(self, i):
"""Update the widget."""
if i >= 0 and not self._addingTab:
self._contentArea.setCurrentWidget(self.tabBar().tabData(i))
def eventFilter(self, obj, event):
"""Intercept events until the correct height is set."""
if event.type() == QtCore.QEvent.Show:
self.plusButton().setFixedHeight(self._tabBar.geometry().height())
self.removeEventFilter(self)
return False
def tabBarLayout(self):
return self._tabBarLayout
def tabBar(self):
return self._tabBar
def plusButton(self):
return self._plusButton
def tabAt(self, point):
"""Get the tab at a given point.
This takes any layout margins into account.
"""
offset = self.layout().contentsMargins().top() + self.tabBarLayout().contentsMargins().top()
return self.tabBar().tabAt(point - QtCore.QPoint(0, offset))
def addTab(self, widget, name=''):
"""Add a new tab.
Returns:
Tab index as an int.
"""
self._addingTab = True
tabBar = self.tabBar()
try:
index = tabBar.addTab(name)
tabBar.setTabData(index, widget)
self._contentArea.addWidget(widget)
finally:
self._addingTab = False
return index
def insertTab(self, index, widget, name=''):
"""Inserts a new tab.
If index is out of range, a new tab is appended.
Returns:
Tab index as an int.
"""
self._addingTab = True
tabBar = self.tabBar()
try:
index = tabBar.insertTab(index, name)
tabBar.setTabData(index, widget)
self._contentArea.insertWidget(index, widget)
finally:
self._addingTab = False
return index
def removeTab(self, index):
"""Remove a tab."""
tabBar = self.tabBar()
self._contentArea.removeWidget(tabBar.tabData(index))
tabBar.removeTab(index)
if __name__ == '__main__':
import sys
import random
app = QtWidgets.QApplication(sys.argv)
test = TabWidgetPlus()
test.addTab(QtWidgets.QPushButton(), 'yeah')
test.insertTab(0, QtWidgets.QCheckBox(), 'what')
test.insertTab(1, QtWidgets.QRadioButton(), 'no')
test.removeTab(1)
test.setMovable(True)
test.setTabsClosable(True)
def tabTest():
name = 'Tab ' + str(random.randint(0, 100))
index = test.addTab(QtWidgets.QLabel(name), name)
test.setCurrentIndex(index)
test.tabOpenRequested.connect(tabTest)
test.tabCloseRequested.connect(lambda index: test.removeTab(index))
test.show()
sys.exit(app.exec_())
The one difference is if you're using tabWidget.tabBar().tabAt(point), this is no longer guaranteed to be correct as it doesn't take any margins into account. I set the margins to 0 so this shouldn't be an issue, but I also included those corrections in TabWidgetPlus.tabAt.
I only copied a few methods from QTabBar to QTabWidget as some may need extra testing.

QT, Python, QTreeView, custom widget, setData - lose reference after drag and drop

This question is similar to the one in this this topic Preserve QStandardItem subclasses in drag and drop but with issue that I cant find a good solution for. That topic partially helps but fail on more complex task.
When I create an item in QTreeView I put that item in my array but when I use drag&Drop the item gets deleted and I no longer have access to it. I know that its because drag and drop copies the item and not moves it so I should use setData. I cant setData to be an object because even then the object gets copied and I lose reference to it.
Here is an example
itemsArray = self.addNewRow
def addNewRow(self)
'''some code with more items'''
itemHolder = QStandardItem("ProgressBarItem")
widget = QProgressBar()
itemHolder.setData(widget)
inx = self.model.rowCount()
self.model.setItem(inx, 0, itemIcon)
self.model.setItem(inx, 1, itemName)
self.model.setItem(inx, 2, itemHolder)
ix = self.model.index(inx,2,QModelIndex())
self.treeView.setIndexWidget(ix, widget)
return [itemHolder, itemA, itemB, itemC]
#Simplified functionality
data = [xxx,xxx,xxx]
for items in itemsArray:
items[0].data().setPercentage(data[0])
items[1].data().setText(data[1])
items[2].data().setChecked(data[2])
The code above works if I won't move the widget. The second I drag/drop I lose reference I lose updates on all my items and I get crash.
RuntimeError: wrapped C/C++ object of type QProgressBar has been deleted
The way I can think of of fixing this problem is to loop over entire treeview recursively over each row/child and on name match update item.... Problem is that I will be refreshing treeview every 0.5 second and have 500+ rows with 5-15 items each. Meaning... I don't think that will be very fast/efficient... if I want to loop over 5 000 items every 0.5 second...
Can some one suggest how I could solve this problem? Perhaps I can edit dropEvent so it does not copy/paste item but rather move item.... This way I would not lose my object in array
Qt can only serialize objects that can be stored in a QVariant, so it's no surprise that this won't work with a QWidget. But even if it could serialize widgets, I still don't think it would work, because index-widgets belong to the view, not the model.
Anyway, I think you will have to keep references to the widgets separately, and only store a simple key in the model items. Then once the items are dropped, you can retrieve the widgets and reset them in the view.
Here's a working demo script:
from PyQt4 import QtCore, QtGui
class TreeView(QtGui.QTreeView):
def __init__(self, *args, **kwargs):
super(TreeView, self).__init__(*args, **kwargs)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setAllColumnsShowFocus(True)
self.setModel(QtGui.QStandardItemModel(self))
self._widgets = {}
self._dropping = False
self._droprange = range(0)
def dropEvent(self, event):
self._dropping = True
super(TreeView, self).dropEvent(event)
for row in self._droprange:
item = self.model().item(row, 2)
self.setIndexWidget(item.index(), self._widgets[item.data()])
self._droprange = range(0)
self._dropping = False
def rowsInserted(self, parent, start, end):
super(TreeView, self).rowsInserted(parent, start, end)
if self._dropping:
self._droprange = range(start, end + 1)
def addNewRow(self, name):
model = self.model()
itemIcon = QtGui.QStandardItem()
pixmap = QtGui.QPixmap(16, 16)
pixmap.fill(QtGui.QColor(name))
itemIcon.setIcon(QtGui.QIcon(pixmap))
itemName = QtGui.QStandardItem(name.title())
itemHolder = QtGui.QStandardItem('ProgressBarItem')
widget = QtGui.QProgressBar()
widget.setValue(5 * (model.rowCount() + 1))
key = id(widget)
self._widgets[key] = widget
itemHolder.setData(key)
model.appendRow([itemIcon, itemName, itemHolder])
self.setIndexWidget(model.indexFromItem(itemHolder), widget)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.treeView = TreeView()
for name in 'red yellow green purple blue orange'.split():
self.treeView.addNewRow(name)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.treeView)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 150, 600, 400)
window.show()
sys.exit(app.exec_())

Categories

Resources