Is it possible to use QInputDialog with nested list or something like that?

599 Views Asked by At

All examples or documentation on PyQt5 QInputDialog I found, used simple classic lists limited to only one item per "row" (like in my code example ("Red","Blue" or "Green")).

I am searching for a good way to build a more detailed multidimensional list, like a table, where the user gets to see and select the whole row (with multiple values as one item) in the input dialog instead of a single value.

For example a nested list like that: [['Ryan', 24, 'm'], ['Lisa', 22, 'f'], ['Joe', 30, 'm']]

--> Imagine each one of the three lists in the list should be one row (entry) in the QInputDialog that can be selected. Like in a table with a checkbox for each row.

Is something like that possible? Anyone knows?

#The normal (limited) version with a simple list I am referring to looks like that:
def getChoice(self):
    itemlist = ("Red","Blue","Green")
    item, okPressed = QInputDialog.getItem(self, "Get item","Color:", itemlist, 0, False)
    if okPressed and item:
        print(item)
1

There are 1 best solutions below

2
On BEST ANSWER

The join() method takes all items in an iterable and joins them into one string.

Syntax: string.join(iterable)

import sys
from PyQt5.QtCore import QTimer
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QLineEdit, QInputDialog, QLabel, QVBoxLayout


class PopupDialog(QtWidgets.QDialog):
    def __init__(self):
        super(PopupDialog, self).__init__()
        self.selected_item = None
        layout = QtWidgets.QFormLayout()
        self.setLayout(layout)
        self.setWindowTitle("New Popup")
        self.setMinimumWidth(400)

#        items = (['Ryan', 24, 'm'], ['Lisa', 22, 'f'], ['Joe', 30, 'm'])
        items = (['Ryan', '24', 'm'], ['Lisa', '22', 'f'], ['Joe', '30', 'm'])
        items = [ " ".join(item) for item in items ]                              # <<<-----<
        item, okPressed = QInputDialog.getItem(self, "Get item",
                          "Color:", items, 0, False)
        if okPressed and item:
            self.selected_item = item


class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setMinimumWidth(600)
        self.setWindowTitle("Main Window")
        self.le = QtWidgets.QLineEdit()
        button = QtWidgets.QPushButton("Select...")
        button.clicked.connect(self.get_input)
        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(self.le)
        layout.addWidget(button)
        self.setLayout(layout)

    def get_input(self):
        popup = PopupDialog()
        print("got selection data: ", popup.selected_item)
        self.le.setText(popup.selected_item)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

enter image description here