如何将项目从一个 window 中的 QListWidget 转移到另一个 window 中的另一个 QListWidget?
How to transfer items from QListWidget based in one window to another QListWidget based in another window?
话虽如此,我希望能够将我的项目从基于一个 window 的 QListWidget 转移到另一个。
下面的代码允许我一次转移一个项目,
但我正在努力想办法转移多个项目
一次。
(以下代码来自我发现并修改过的示例。)
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QDialog,
QWidget,
QVBoxLayout,
QLineEdit,
QLabel,
QPushButton,
QListWidget,
QAbstractItemView
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.user_input = QListWidget()
self.user_input.addItem("2")
self.user_input.addItem("3")
self.populate()
self.show()
self.user_input.setSelectionMode(QAbstractItemView.ExtendedSelection)
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()
for widget in widgets: layout.addWidget(widget)
parentWidget.setLayout(layout)
return parentWidget
def when_input(self, function):
#self.user_input.textChanged.connect(function)
self.user_input.itemClicked.connect(self.printItemText)
self.user_input.itemClicked.connect(function)
def printItemText(self):
items = self.user_input.selectedItems()
x = []
for i in range(len(items)):
x.append(str(self.user_input.selectedItems()[i].text()))
print (x)
class Dialog(QDialog):
def __init__(self):
super().__init__()
self.user_input = QListWidget()
self.relay_sum = None # function to relay result of addition
self.populate()
self.show()
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 main():
app = QApplication([])
mainWindow = MainWindow()
dialog = Dialog()
mainWindow.when_input(lambda text: dialog.user_input.addItem(str(text.text())))
app.exec_()
if __name__ == "__main__":
main()
单击某个项目时,它也会被选中,因此只需使用最后一个功能即可。
一种可能的解决方案是创建一个新的令牌,用于传输所选项目中的克隆项目,然后将其添加到另一个 QListWidget。
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import (
QAbstractItemView,
QApplication,
QDialog,
QLabel,
QListWidget,
QMainWindow,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
custom_signal = pyqtSignal(list)
def __init__(self):
super().__init__()
self.user_input = QListWidget(selectionMode=QAbstractItemView.ExtendedSelection)
self.user_input.addItem("2")
self.user_input.addItem("3")
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):
items = []
for item in self.user_input.selectedItems():
items.append(item.clone())
self.custom_signal.emit(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:
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()
话虽如此,我希望能够将我的项目从基于一个 window 的 QListWidget 转移到另一个。
下面的代码允许我一次转移一个项目, 但我正在努力想办法转移多个项目 一次。
(以下代码来自我发现并修改过的示例。)
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QDialog,
QWidget,
QVBoxLayout,
QLineEdit,
QLabel,
QPushButton,
QListWidget,
QAbstractItemView
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.user_input = QListWidget()
self.user_input.addItem("2")
self.user_input.addItem("3")
self.populate()
self.show()
self.user_input.setSelectionMode(QAbstractItemView.ExtendedSelection)
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()
for widget in widgets: layout.addWidget(widget)
parentWidget.setLayout(layout)
return parentWidget
def when_input(self, function):
#self.user_input.textChanged.connect(function)
self.user_input.itemClicked.connect(self.printItemText)
self.user_input.itemClicked.connect(function)
def printItemText(self):
items = self.user_input.selectedItems()
x = []
for i in range(len(items)):
x.append(str(self.user_input.selectedItems()[i].text()))
print (x)
class Dialog(QDialog):
def __init__(self):
super().__init__()
self.user_input = QListWidget()
self.relay_sum = None # function to relay result of addition
self.populate()
self.show()
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 main():
app = QApplication([])
mainWindow = MainWindow()
dialog = Dialog()
mainWindow.when_input(lambda text: dialog.user_input.addItem(str(text.text())))
app.exec_()
if __name__ == "__main__":
main()
单击某个项目时,它也会被选中,因此只需使用最后一个功能即可。
一种可能的解决方案是创建一个新的令牌,用于传输所选项目中的克隆项目,然后将其添加到另一个 QListWidget。
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import (
QAbstractItemView,
QApplication,
QDialog,
QLabel,
QListWidget,
QMainWindow,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
custom_signal = pyqtSignal(list)
def __init__(self):
super().__init__()
self.user_input = QListWidget(selectionMode=QAbstractItemView.ExtendedSelection)
self.user_input.addItem("2")
self.user_input.addItem("3")
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):
items = []
for item in self.user_input.selectedItems():
items.append(item.clone())
self.custom_signal.emit(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:
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()