Skip to content

Instantly share code, notes, and snippets.

@anddam
Created March 5, 2021 00:41
Show Gist options
  • Save anddam/5a41eddb28130c4b9032571e3812f559 to your computer and use it in GitHub Desktop.
Save anddam/5a41eddb28130c4b9032571e3812f559 to your computer and use it in GitHub Desktop.
QListWidget custom widget in item
"""Try to use a custom widget in a QListWidgetItem, expected result is test-row.ui widget in each row.
Actual result is blank lines.
Require PySide2, tested on Python 3.8.6
"""
import sys
from PySide2.QtWidgets import (
QApplication,
QHBoxLayout,
QListWidget,
QListWidgetItem,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
from PySide2.QtCore import QFile, Slot
from PySide2.QtUiTools import QUiLoader
def load_ui_widget(uifilename, parent=None):
loader = QUiLoader()
uifile = QFile(uifilename)
uifile.open(QFile.ReadOnly)
ui = loader.load(uifile, parent)
uifile.close()
return ui
class DeviceListWidgetItem(QWidget):
def __init__(self, ui_file, *args, **kwargs):
print("DeviceListWidgetItem inited ui_file: %r" % ui_file)
super().__init__(*args, **kwargs)
self.resize(400, 50)
self.ui = load_ui_widget(ui_file, None)
main_layout = QHBoxLayout()
main_layout.addWidget(self.ui)
self.setLayout(main_layout)
def set_remove_callback(self, function):
self.ui.remove_button.clicked.connect(lambda x=self: function(x))
class MyMainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_button = QPushButton("add item")
self.print_button = QPushButton("print item 0 label")
self.list_widget = QListWidget()
self.layout = QVBoxLayout()
self.widget = QWidget()
self.layout.addWidget(self.add_button)
self.layout.addWidget(self.print_button)
self.layout.addWidget(self.list_widget)
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
self.add_button.clicked.connect(self.add_button_clicked)
self.print_button.clicked.connect(self.print_button_clicked)
@Slot(bool)
def add_button_clicked(self, checked=False):
print("add_buttonClicked checked:%r" % checked)
widget = DeviceListWidgetItem(ui_file="test-row.ui")
item = QListWidgetItem()
self.list_widget.insertItem(self.list_widget.count(), item)
self.list_widget.setItemWidget(item, widget)
item.setSizeHint(widget.sizeHint())
@Slot(bool)
def print_button_clicked(self, checked=False):
print("print_buttonClicked")
print(self.list_widget.itemWidget(self.list_widget.item(0)).ui.label.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MyMainWindow()
main_window.add_button_clicked()
main_window.show()
sys.exit(app.exec_())
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>219</width>
<height>32</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>208</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Widgety widget</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Widgety Button</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment