Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to share the same widget

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_())
like image 286
alphanumeric Avatar asked Mar 20 '23 02:03

alphanumeric


2 Answers

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.

like image 188
Hyperboreus Avatar answered Apr 01 '23 01:04

Hyperboreus


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_())
like image 26
alphanumeric Avatar answered Apr 01 '23 03:04

alphanumeric