Форум сайта python.su
Не могу разобраться как тот же код реализовать из Tkinter в Qt, пока без понятия как выделить элементы QListView и как перехватывать нажатие клавиш в QComboBox (по крайней мере без создания подкласса QComboBox и обработки всех событий в event)
пример кода на Tkinter
from Tkinter import Tk, Frame, Scrollbar, Listbox from ttk import Combobox def listbox_select(event): value = event.widget.get(event.widget.nearest(event.y)) if value in values_select: del values_select[values_select.index(value)] else: values_select.append(value) def combobox_key_release(event): if event.keycode == 13 and combobox.get() and combobox.get() not in values: values.append(combobox.get()) values.sort() listbox_filling() def combobox_click(event): combobox['values'] = [_ for _ in values if combobox.get() in _] def listbox_filling(): listbox.delete(0, 'end') for index, text in enumerate(filter(lambda _: combobox.get() in _, values)): listbox.insert('end', text) listbox_set_select() def listbox_set_select(): for index, text in enumerate(listbox.get(0, 'end')): if text in values_select: listbox.selection_set(index, index) values = ["text " + str(_).rjust(3, '0') for _ in range(201)] values_select = [] root = Tk() combobox = Combobox(root, value=values) combobox.pack(side='top', fill='x', expand=False) combobox.bind('<1>', combobox_click) combobox.bind('<KeyRelease>', combobox_key_release) frame = Frame(root) frame.pack(side='top', fill='both', expand=True) scroll = Scrollbar(frame) scroll.pack(side='right', fill='y', expand=False) listbox = Listbox(frame, selectmode='multiple', selectbackground='#f00') listbox.pack(side='left', fill='both', expand=True) listbox.bind("<1>", listbox_select) listbox.bind('<Enter>', listbox_filling()) listbox_filling() scroll['command'] = listbox.yview listbox['yscrollcommand'] = scroll.set root.mainloop()
from PyQt5 import QtWidgets, QtCore, QtGui import tkinter values = ["text " + str(_).rjust(3, '0') for _ in range(211)] values_select = [] def combobox_change_text(event): sti_filling() def sti_filling(): sti.clear() for i, _ in enumerate(filter(lambda _: combobox.currentText() in _, values)): sti.appendRow(QtGui.QStandardItem(_)) def list_view_clicked(index): item = sti.itemFromIndex(index) if item in values_select: del(values_select[values_select.index(item)]) else: values_select.append(item) def _list_view_clicked(event): if event.data() in values_select: del(values_select[values_select.index(event.data())]) else: values_select.append(event.data()) app = QtWidgets.QApplication([]) window = QtWidgets.QWidget() vbox = QtWidgets.QVBoxLayout() vbox.setContentsMargins(1, 1, 1, 1) vbox.setSpacing(1) combobox = QtWidgets.QComboBox() vbox.addWidget(combobox) combobox.addItems(values) combobox.setEditable(True) combobox.editTextChanged.connect(combobox_change_text) list_view = QtWidgets.QListView() vbox.addWidget(list_view) sti = QtGui.QStandardItemModel(parent=window) combobox.setCurrentText('') sti_filling() list_view.setModel(sti) list_view.setSelectionMode(2) list_view.clicked.connect(list_view_clicked) window.setLayout(vbox) window.show() app.exec_()
Офлайн
переделал без QStandardItemModel
from PyQt5 import QtWidgets, QtCore, QtGui import tkinter values = ["text " + str(_).rjust(3, '0') for _ in range(211)] values_select = [] def combobox_change_text(event): list_widget_filling() list_widget_select() def list_widget_filling(): list_widget.clear() for i, _ in enumerate(filter(lambda _: combobox.currentText() in _, values)): list_widget.addItem(_) def list_widget_clicked(event): if event.data() in values_select: del(values_select[values_select.index(event.data())]) else: values_select.append(event.data()) def list_widget_select(): for index in range(list_widget.count()): if list_widget.item(index).text() in values_select: list_widget.item(index).setSelected(1) app = QtWidgets.QApplication([]) window = QtWidgets.QWidget() vbox = QtWidgets.QVBoxLayout() vbox.setContentsMargins(1, 1, 1, 1) vbox.setSpacing(1) combobox = QtWidgets.QComboBox() vbox.addWidget(combobox) combobox.addItems(values) combobox.setEditable(True) combobox.editTextChanged.connect(combobox_change_text) list_widget = QtWidgets.QListWidget() list_widget.setSelectionMode(2) vbox.addWidget(list_widget) list_widget.addItems(values) list_widget.clicked.connect(list_widget_clicked) window.setLayout(vbox) window.show() app.exec_()
Офлайн