how to sort a list of custom widgets in PyQt5? - pyqt5

I have a custom widget with two labels LabelA and LabelB in it. This is added into a QlistWidget. How to sort the list based LabelA or LabelB depending on the user requirements? I would like to keep labelA and labelB in a tile (styled widget)
I saw this solution on here
class ListWidgetItem(QtGui.QListWidgetItem):
def __lt__(self, other):
return self.text() < other.text() # or whatever
But i would like to pass an additional argument to __lt__

Related

How to resize QTableWidgetItem to its editor size then to its text size using QStyledItemDelegate?

I created my own CustomDelegate class derived from QStyledItemDelegate:
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor_hlayout = QHBoxLayout(editor)
button = QPushButton()
line_edit = QLineEdit()
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(line_edit)
return editor
def setEditorData(self, editor, index):
model_data = index.model().data(index, Qt.EditRole)
editor.layout().itemAt(1).widget().setText(model_data) # Set line_edit value
def setModelData(self, editor, model, index):
editor_data = editor.layout().itemAt(1).widget().text() # Get line_edit value
model.setData(index, editor_data, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
I set it for the last column of the QTableWidget my_table :
my_delegate = CustomDelegate(my_window)
my_table.setItemDelegateForColumn(my_table.columnCount()-1, my_delegate)
To be precise, my goal is to edit the table widget item size when double-clicking it so that it fits the editor size and properly displays it, then edit the table widget item size right after exiting editor mode so that it fits its text size back again.
To that end, I added the lines index.model().setData(index, editor.sizeHint(), Qt.SizeHintRole) in createEditor method and model.setData(index, QTableWidgetItem(editor_data).sizeHint(), Qt.SizeHintRole) in setModelData method.
The problem is that QTableWidgetItem(editor_data).sizeHint() returns (-1, -1) size. I also tried with QTextDocument(editor_data).size() but it does not fit the text width (it is slightly smaller).
How to get the good text size hint ?
Also, somebody told me about reducing the editor minimum size hint to avoid editing the QTableWidgetItem size at all. Does it solve my problem and how to perform it ?
The editor of a item view should not change the size of its index. The only cases for which this is considered valid is for persistent editors and index widgets, but due to their nature it makes sense: they are expected to persist on the view, and not only it's acceptable that they require the view to eventually expand their row or column, but also necessary in order to avoid editors hiding other items (or editors).
Any change in the section sizes can be potentially very demanding to the view, especially if it has lots of data and any of the header uses the ResizeToContents mode, that's why the default factory editors (QLineEdit, QDate/TimeEdit and Q[Double]SpinBox) don't update the index sizes but eventually extend their geometry temporarily.
I would suggest to follow this practice, and eventually update the geometry in updateEditorGeometry() according to the editor position.
In order to optimize the available space, you could use some precautions:
explicitly set 0 margins for the layout;
minimize the spacing between widgets;
disable the frame of the QLineEdit (as the default editor does);
use QToolButton instead of QPushButton, since it can usually be made smaller than the latter; this also makes it easier some focus aspects, as QToolButton doesn't accept focus by clicking;
Also note that:
the editor should use setAutoFillBackground(True) otherwise parts of the underlying items might be visible;
the line edit should probably have the focus on default, you can use setFocusProxy() on the editor, so that the line edit gets focused when the parent does;
you should not use the layout to access the line edit, instead create a reference for it in the editor object;
if you still want to make the editor border visible, use a proper stylesheet;
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.setAutoFillBackground(True)
editor_hlayout = QHBoxLayout(editor)
editor_hlayout.setContentsMargins(0, 0, 0, 0)
editor_hlayout.setSpacing(1)
button = QToolButton()
editor.line_edit = QLineEdit(frame=False)
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(editor.line_edit)
editor.setFocusProxy(editor.line_edit)
# eventually (see note)
editor.setObjectName('delegateEditor')
editor.setStyleSheet('''
#delegateEditor {
border: 1px solid palette(mid);
background: palette(base);
}
''')
return editor
def setEditorData(self, editor, index):
model_data = index.model().data(index, Qt.EditRole)
editor.line_edit.setText(model_data)
def setModelData(self, editor, model, index):
editor_data = editor.line_edit.text()
model.setData(index, editor_data, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
super().updateEditorGeometry(editor, option, index)
editor.resize(editor.sizeHint().width(), editor.height())
rect = editor.geometry()
parentRect = editor.parent().rect()
if not parentRect.contains(rect):
if rect.right() > parentRect.right():
rect.moveRight(parentRect.right())
if rect.x() < parentRect.x():
rect.moveLeft(parentRect.x())
if rect.bottom() > parentRect.bottom():
rect.moveBottom(parentRect.bottom())
if rect.y() < parentRect.y():
rect.moveTop(parentRect.y())
editor.setGeometry(rect)
Note: if you do use stylesheets, it's possible that the line edit will partially draw over the border; in that case, use editor_hlayout.setContentsMargins(0, 0, 1, 0).
That said, if you really want to update the view sizes, you still can, but it can be a bit tricky.
The trick is to keep track of the editor and its index, manually resize the sections in order to fit its size, and then restore those sizes when the editor is destroyed.
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent):
self.editorData = {}
super().__init__(parent)
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.setAutoFillBackground(True)
editor_hlayout = QHBoxLayout(editor)
editor_hlayout.setContentsMargins(0, 0, 0, 0)
editor_hlayout.setSpacing(1)
button = QToolButton()
editor.line_edit = QLineEdit(frame=False)
editor_hlayout.addWidget(button)
editor_hlayout.addWidget(editor.line_edit)
editor.setFocusProxy(editor.line_edit)
view = option.widget
# store the editor, the view and also the current sizes
self.editorData[index] = (editor, view,
view.horizontalHeader().sectionSize(index.column()),
view.verticalHeader().sectionSize(index.row())
)
# THEN, resize the row and column, which will call sizeHint()
view.resizeColumnToContents(index.column())
view.resizeRowToContents(index.row())
# delay a forced scroll to the index to ensure that the editor is
# visible after the sections have been resized
QTimer.singleShot(1, lambda: view.scrollTo(index))
return editor
def sizeHint(self, opt, index):
if index in self.editorData:
# sizeHint doesn't provide access to the editor
editor, *_ = self.editorData[index]
return editor.sizeHint()
return super().sizeHint(opt, index)
def destroyEditor(self, editor, index):
super().destroyEditor(editor, index)
if index in self.editorData:
editor, view, width, height = self.editorData.pop(index)
view.horizontalHeader().resizeSection(index.column(), width)
view.verticalHeader().resizeSection(index.row(), height)
Note that if the index is near the edge (last row or column), when the editor is destroyed it's possible that the view won't properly adjust its scroll bars if the scroll mode is ScrollPerItem. AFAIK there's no easy workaround for this.
Remember what said above, though. As Ian Malcolm would say, whether this could be done or not, you should stop and think if you should.
Update about focus issues
As pointed out in comments, if the user clicks somewhere else when the editor is active, the editor doesn't close itself as expected.
By default, the delegate's event filter closes the editor whenever it loses focus, but the filter is set on the editor, not its children. When the line edit receives focus, the event filter recognizes that the new focus widget is a child of the editor and will not close it; this is important, because if the editor has child widgets, you don't want it to close just because the internal focus has changed; the side effect of this is that when clicking outside of the line edit, it is the line edit that will receive the FocusOut event, not the editor, so the event filter won't know nothing about it; note that this also happens even when using setFocusProxy(), as Qt automatically sends focus events to the proxy.
There are at least two possible solutions for this, depending on the editor type.
If there is going to be just one main child widget that should accept focus like in this case, the solution is to do what other complex widgets (like QComboBox and QAbstractSpinBox): set the editor as focus proxy of the widget, and post the related events to the widget in the event filter:
def createEditor(self, parent, option, index):
# ...
editor.line_edit.setFocusProxy(editor)
def eventFilter(self, editor, event):
if event.type() in (
event.FocusIn, event.FocusOut,
event.KeyPress, event.KeyRelease,
event.ShortcutOverride, event.InputMethod,
event.ContextMenu
):
editor.line_edit.event(event)
if event.type() != event.FocusOut:
return event.isAccepted()
return super().eventFilter(editor, event)
For more complex situations, we need to ensure that the default implementation of the delegate event filter receives a focus out event whenever the focus is completely lost by the editor or any of its children.
In order to achieve this, a possible solution is to create a custom object that will become an event filter for all child widgets of the editor and eventually posts a FocusOut event for the editor whenever the focused widget is not itself or one of its children (including grandchildren).
class FocusOutFilter(QObject):
def __init__(self, widget):
super().__init__(widget)
self.widget = widget
self.install(widget)
def install(self, widget):
widget.installEventFilter(self)
for child in widget.findChildren(QWidget):
child.installEventFilter(self)
def eventFilter(self, obj, event):
if (event.type() == event.FocusOut and
not self.widget.isAncestorOf(QApplication.focusWidget())):
obj.removeEventFilter(self)
self.deleteLater()
return QApplication.sendEvent(self.widget, event)
elif event.type() == event.ChildAdded:
self.install(event.child())
return super().eventFilter(obj, event)
class CustomDelegate(QStyledItemDelegate):
# ...
def createEditor(self, parent, option, index):
editor = QWidget(parent)
editor.filter = FocusOutFilter(editor)
# ...
There is a catch. The problem is that focusWidget() will return the widget even if the event results in opening a menu. While this is fine for any context menu of the editor widgets (including the edit menu of QLineEdit), it could create some issues when the view implements the context menu event (showing a menu in contextMenuEvent(), within a ContextMenu event or with CustomContextMenu policy). There is no final solution for this, as there is no definite way to check what created the menu: a proper implementation expects that a QMenu is created with the parent that created it, but that cannot be certain (for simplicity, dynamically created menus could be created without any parent, which makes it impossible to know what created them). In these cases, the simplest solution is to implement the above conditions by first checking the return value of isPersistentEditorOpen() and ensure that widgetAt() properly returns the table's viewport().

How to use a for loop to iterate over dynamically named variables (var1, var2, ...)

I am trying to make a GUI using PyQt5.
I have a couple of buttons say 4 which call the same function. A click on a button calls the same function to plot the data associated with each button.
radiobutton1.clicked.connect(self.fun)
radiobutton2.clicked.connect(self.fun)
radiobutton3.clicked.connect(self.fun)
radiobutton4.clicked.connect(self.fun)
Since each line of code is the same except the name of the button, is there a way instead of writing almost the same code four times, I use a for loop to generate the code?
You should initialize them in a loop, then append all of the objects to a list. Then, in this case, all you'd have to do is iterate over the list and call the .clicked.connect() method on each object.
radio_buttons = list()
for i in range(1, 5):
temp_radio_button = QRadioButton("Radio Button")
radio_buttons.append(temp_radio_button)
....
for radio_button in radio_buttons:
radio_button.clicked.connect(self.fun)
I know it does not apply to this case, but for those who use QtDesigner and need to access the widgets later, this can be an option:
def get_all_objects(self,label,window,widget_class):
for widget in filter(lambda entry: match(label,entry.objectName()),window.findChildren(widget_class)):
yield widget
for widget in self.get_all_objects('radiobutton',MainWindow,QtWidgets.QRadioButton):
widget.clicked.connect(self.fun)
Note:MainWindow can be replaced by a page on a StackedWidget if all your target widgets are in the same page.

PyQt 5 Hiding and showing widgets an dealing with List Widgets

I am pretty new to Python and very new to PyQt. Just trying to find a solution on how to hide widgets that do not need to be shown until a button is clicked. Also can't figure out the syntax with items in a list widget. If x is clicked in a list widget it will execute y.
If you want to trigger a slot when clicking items in QListWidget,
you can use itemClicked signal to accomplish it,
Hear is an example,
class YourListWidget(QListWidget):
def __init__(self, parent=None):
super(YourListWidget, self).__init__(parent)
self.itemClicked.connect(self.do_something)
def do_something(self, item):
# item is the item you clicked, it is a QListWidgetItem object
# you can see its name by calling text() method of QListWidgetItem,
# or do anything you want in this slot.
print(item.text())

How to change right-arrow icon to access hidden menu items in QMenuBar using PyQt5?

I found no references about this in the documentation.
You cannot easily style this "extension" button because that symbol is actually an icon.
You can however access the QToolButton widget to set the icon to whatever you like. In PyQt4 you get to it with menubar.children()[0]. This should be consistent with PyQt5. Looking at the Qt5 source code, it appears that the extension icon is always created first, even if it is not shown, and the children() method returns objects in the order in which they were created (this the index of 0).
Once you have a reference to the QToolButton, you can then set the icon to whatever you like with menubar.children()[0].setIcon(my_qicon) (or similar).
Since this is one of the top items on google for modifying the "show more" icon:
Another option is to use a QToolbar. You can do the same thing except that the first child is a layout, second is the QToolButton that you want:
from qtpy import QtWidgets, QtGui
import sys
def call_back():
print('pressed')
app = QtWidgets.QApplication([])
widget = QtWidgets.QWidget()
layout = QtWidgets.QGridLayout(widget)
toolbar = QtWidgets.QToolBar()
layout.addWIdget(toolbar)
# add some actions
for i in range(10):
toolbar.addAction('test_{}'.format(i), call_back)
# change the icon, the first child is a layout!, the second it the toolbtn we want!
toolbar.children()[1].setIcon(QtGui.QIcon('path/to/image.png'))
widget.show()
app.exec_()
sys.exit()

PyQt Table Header Context Menu

I am working on a QTableView displaying a custom model subclasses from QAbstractTableModel in PyQt4. I need to be able to allow the user to set which column they want to serve as a specific type of data. To achieve this I want to implement a context menu when clicking on the header of a column, allowing options to set it as these types. I'm not sure how to create a context menu like this which can differentiate between different columns. Can anyone point me in the right direction?
Thanks
You can access the information from the header view. You can do something like:
def __init__( self, parent ):
# initialize class
...
# setup menu options
header = self.ui.tree.header()
header.setContextMenuPolicy(Qt.CustomContextMenu)
header.customContextMenuRequested.connect( self.showHeaderMenu )
def showHeaderMenu( self, point ):
column = self.ui.tree.header().logicalIndexAt(point.x())
# show menu about the column
menu = QMenu(self)
menu.addAction('Hide Column')
menu.popup(header.mapToGlobal(pos))