2025-03-05 14:42:16 +01:00

27 lines
1.3 KiB
Python

from PyQt6.QtWidgets import QWidget, QVBoxLayout, QListWidget, QListWidgetItem, QLabel, QDialogButtonBox, QDialog
from typing import Callable
# QT6 non-window that presents a list of devices of which one can be connected
class ListChoice(QDialog):
def __init__(self, items: dict[str, list[str]], parent=None, window_title="Select Device"):
super().__init__(parent)
self.setWindowTitle(window_title)
self.setLayout(QVBoxLayout())
self.layout().addWidget(QLabel("Select a device to connect to:"))
self.device_list = QListWidget()
self.layout().addWidget(self.device_list)
for key, items in items.items():
for item in items:
w = QListWidgetItem(f"{key}: {item}", parent=self.device_list)
w.key = key
w.item = item
# self.device_list.addItem(f"{key}: {item}")
self.buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
self.layout().addWidget(self.buttons)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
def get_selected(self):
selected_item = self.device_list.currentItem()
return selected_item.key, selected_item.item