I have a main window with a mdiArea in it. In the area there are 2 subwindows, and possibly more. I want to have everyone have a QListWidget that shares the information and they are synchronized. I searched but I can not find any help.
I was thinking of something like a static list, I want to share the data between all the sub-windows and allow anyone to work on them, 1 window at a time
A widget can only have one parent and it was drawn in the window that belongs to the parent, so the answer to your direct question is that it can not. But I think that in the background you want several views to share the same data and be synchronized, if so, the solution is to use a proxy so that several models share the same data at all times.
In the case of the QListWidget you can not set a model but it will be the one used as a base and the widgets copies will be QListView.
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
mdiarea = QtWidgets.QMdiArea()
self.setCentralWidget(mdiarea)
list_widget = QtWidgets.QListWidget()
for i in range(10):
it = QtWidgets.QListWidgetItem('item {}'.format(i))
it.setFlags(it.flags() | QtCore.Qt.ItemIsEditable)
list_widget.addItem(it)
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_widget)
mdiarea.addSubWindow(sub_window)
for _ in range(4):
list_view = self.create_qlistview(list_widget.model())
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_view)
mdiarea.addSubWindow(sub_window)
def create_qlistview(self, model):
proxy = QtCore.QIdentityProxyModel()
proxy.setSourceModel(model)
list_view = QtWidgets.QListView()
list_view.setModel(proxy)
return list_view
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())