Issue
I have 2 windows with QListWidgets. One is populated while the other is empty. When an item is selected in the main window it will appear in the secondary window. The items that are selected and appear in the secondary window should match the order they have in the main window.
For example if I select E -> D -> C -> B -> A in the main window the secondary window should display A, B, C, D, E from top to bottom.
How can this be accomplished?
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import (
QAbstractItemView,
QApplication,
QDialog,
QLabel,
QListWidget,
QMainWindow,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
custom_signal = pyqtSignal(list)
items = []
def __init__(self):
super().__init__()
self.emitted_items = []
self.user_input = QListWidget(selectionMode=QAbstractItemView.ExtendedSelection)
self.user_input.addItem("a")
self.user_input.addItem("b")
self.user_input.addItem("c")
self.user_input.addItem("d")
self.user_input.addItem("e")
self.populate()
self.user_input.itemSelectionChanged.connect(self.handle_selection_changed)
def populate(self):
widgets = [QLabel("Insert a number"), self.user_input]
centralWidget = self.group_widgets(widgets)
self.setCentralWidget(centralWidget)
def group_widgets(self, widgets):
parentWidget = QWidget()
layout = QVBoxLayout(parentWidget)
for widget in widgets:
layout.addWidget(widget)
return parentWidget
def handle_selection_changed(self):
for item in self.user_input.selectedItems():
#print(item.text())
if item.text() not in self.items:
self.items.append(item.text())
#print(self.items)
for x in self.emitted_items:
#print('this is x: ' + str(x))
if x in self.items:
self.items.remove(x)
for y in self.items:
if y not in self.emitted_items:
self.emitted_items.append(y)
print('the emitted items: ' + str(self.emitted_items))
self.custom_signal.emit(self.items)
class Dialog(QDialog):
def __init__(self):
super().__init__()
self.user_input = QListWidget()
self.populate()
def populate(self):
widgets = self.get_widgets()
layout = self.get_layout(widgets)
self.setLayout(layout)
def get_widgets(self):
widgets = [
QLabel("Inserted number"),
self.user_input,
]
return widgets
def get_layout(self, widgets):
layout = QVBoxLayout()
for widget in widgets:
layout.addWidget(widget)
return layout
def add_items(self, items):
for item in items:
#if item not in items:
#print('this is item: ' + str(item))
#print('these are the items: ' + str(items))
self.user_input.addItem(item)
def main():
app = QApplication([])
mainWindow = MainWindow()
mainWindow.show()
dialog = Dialog()
dialog.show()
mainWindow.custom_signal.connect(dialog.add_items)
app.exec_()
if __name__ == "__main__":
main()
Solution
After parsing the strings I wanted from a .txt
file and appending them to the read_lines
list, I used the read_lines
list to populate both of my QListWidgets
.
Then I hid the items in the QListWidget
that belongs to the main_window
.
Since the same list is used to populate both QListWidgets
, items in both widgets will be ordered in the same way as their corresponding elements are ordered in the read_lines
list.
Knowing this, the indexes of the items that are selected in the secondary_window QListWidget
can be used to reveal the correct items in the main_window QListWidget
.
self.secondary_window.string_selection_list_box.addItems(read_lines)
self.main_window.microbase_list_box.addItems(read_lines)
for element in range(len(self.main_window.microbase_list_box)):
self.main_window.microbase_list_box.item(element).setHidden(True)
for string in selected_strings:
index = read_lines.index(string)
self.main_window.microbase_list_box.item(index).setHidden(False)
Answered By - ToonzNCereal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.