There is QTabWidget with two Tabs: 'A' and 'B'.
And there is a listWidgetA = QtGui.QListWidget() which I want to share among 'A' and 'B'.
So far I was unable to find a way to assign listWidgetA to two different layouts. Assigning it to one layout immediately cancels an assignment to another. I don't want to create another QtGui.QListWidget() since it would produce a lot of duplicate/repetitive code. Here is the example. It would be interesting to know if there is solution to situation like this.
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myTabWidget = QtGui.QTabWidget()
QGroupBoxA = QtGui.QGroupBox()
QHBoxLayoutA = QtGui.QHBoxLayout()
QGroupBoxA.setLayout(QHBoxLayoutA)
myTabWidget.addTab(QGroupBoxA,' Tab A ')
listWidgetA = QtGui.QListWidget()
for i in range(12):
QtGui.QListWidgetItem( 'Item '+str(i), listWidgetA )
QHBoxLayoutA.addWidget(listWidgetA)
QGroupBoxB = QtGui.QGroupBox()
QHBoxLayoutB = QtGui.QHBoxLayout()
QGroupBoxB.setLayout(QHBoxLayoutB)
# QHBoxLayoutB.addWidget(listWidgetA)
myTabWidget.addTab(QGroupBoxB,' Tab B ')
self.setCentralWidget(myTabWidget)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
As far as I know, each widget has exactely one parent.
You could consider using two QListView
widgets (instead of QListWidget
) which share one and only one model. This would avoid your concern about duplicate/repetitive code.
Thanks guys! Here it's in action (after dialog opens click on tab to let it do business):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myTabWidget = QtGui.QTabWidget()
self.QGroupBoxA = QtGui.QGroupBox()
myTabWidget.addTab(self.QGroupBoxA,' Tab A ')
self.QGroupBoxB = QtGui.QGroupBox()
myTabWidget.addTab(self.QGroupBoxB,' Tab B ')
self.QHBoxLayout = QtGui.QHBoxLayout()
self.listWidget = QtGui.QListWidget()
self.QHBoxLayout.addWidget(self.listWidget)
myTabWidget.connect(myTabWidget, QtCore.SIGNAL("currentChanged(int)"), self.tabSelected)
self.setCentralWidget(myTabWidget)
def tabSelected(self, arg=None):
self.listWidget.clear()
if arg==0:
self.QGroupBoxA.setLayout(self.QHBoxLayout)
for i in range(12):
QtGui.QListWidgetItem( 'A Item '+str(i), self.listWidget )
if arg==1:
self.QGroupBoxB.setLayout(self.QHBoxLayout)
for i in range(12):
QtGui.QListWidgetItem( 'B Item '+str(i), self.listWidget )
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With