4

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_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

2 Answers2

4

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.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • Aside from QListWidget the dialog has few other widgets.. But thanks for a trick. – alphanumeric Mar 24 '14 at 19:44
  • @Sputnix In this case a dirty, dirty work-around would be, to change the widget's parent, each time the user switches tabs. I would not advocate this. – Hyperboreus Mar 24 '14 at 19:47
  • Why not? It sounds great to me! – alphanumeric Mar 24 '14 at 19:50
  • Do the widget and its clone share the same state or do they just look the same? – Hyperboreus Mar 24 '14 at 19:51
  • The ListWidget gets populated with a different data with different tab selected. Regardless of the data used to populate the ListWidget clicking ListItems triggers the same function - a reason I don't want to deal with another ListWidget. – alphanumeric Mar 24 '14 at 20:09
  • Then the situation is different. Implement ONE widget, with all its funcionality in this class. Then instatiate it twice. So you write the code only once, and have two independent widgets. – Hyperboreus Mar 24 '14 at 20:10
  • Thanks Hyeperboreus! Please take a look at the code posted below. Thanks again! – alphanumeric Mar 24 '14 at 20:27
3

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_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392