Frage

With a row of QListWidgets I wonder if there is a simple way to label each so the user would be able to say which List is which. Here is a dialog's screenshot a code posted below results. I avoid using any widgets outside of QListWidgets(). Ideal solution would be solved utilizing QListWidgets itself. It would be great if there is a way to place a text line-label similar to those available for QGroupBox with .setTitle('myString'). At very least an ability to place a label as a first list item would be sufficient too...



from PyQt4 import QtGui, QtCore

class MyApp(object):
    def __init__(self):
        super(MyApp, self).__init__()
        app = QtGui.QApplication(sys.argv)
        self.mainWidget = QtGui.QWidget()
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.groupbox = QtGui.QGroupBox()
        self.groupbox.setTitle('My Groupbox')
        self.layout = QtGui.QVBoxLayout()
        self.groupbox.setLayout(self.layout)

        self.listGroupbox = QtGui.QGroupBox()
        self.listLayout = QtGui.QHBoxLayout()
        self.listGroupbox.setLayout(self.listLayout)
        self.listA=QtGui.QListWidget()
        self.listB=QtGui.QListWidget()

        self.listLayout.addWidget(self.listA)
        self.listLayout.addWidget(self.listB)
        self.layout.addWidget(self.listGroupbox) 

        self.okButton = QtGui.QPushButton('OK')
        self.okButton.clicked.connect(self.OK) 
        self.layout.addWidget(self.okButton)                      
        self.mainLayout.addWidget(self.groupbox)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def OK(self):
        print 'Ok'    
if __name__ == '__main__':
    MyApp()

enter image description here

War es hilfreich?

Lösung

Unfortunately there's no (Qt-native) way to label a QListView (on which QListWidget is based). If your really don't want additional widgets, I would instead use a single-column QTableWidget, and put the list title in the column header. QTableWidget and QListWidget work pretty similarly, so this probably won't break too much of your existing code.

An example based on yours:

class MyApp(object):
    def __init__(self):

        # snipped

        self.listA = self.prepareTableWidget('List A')
        self.listB = self.prepareTableWidget('List B')

        # snipped

    def prepareTableWidget(self, name):
        table = QtGui.QTableWidget()
        table.setColumnCount(1)
        table.setHorizontalHeaderLabels([name])
        table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        return table

    # snipped

QTableWidget example

Andere Tipps

Here is my attempt to achieve it without abandoning QListWidget()... utilizing layout's .insertLayout() method to attach QLabel without losing GUI space usually taken by QGroupBox()...

enter image description here


from PyQt4 import QtGui, QtCore

class MyApp(object):
    def __init__(self):
        super(MyApp, self).__init__()
        app = QtGui.QApplication(sys.argv)
        self.mainWidget = QtGui.QWidget()
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.groupbox = QtGui.QGroupBox()
        self.groupbox.setTitle('My Groupbox')
        self.layout = QtGui.QVBoxLayout()
        self.groupbox.setLayout(self.layout)

        self.listGroupbox = QtGui.QGroupBox()
        self.listLayout = QtGui.QHBoxLayout()
        self.listGroupbox.setLayout(self.listLayout)
        self.listA=QtGui.QListWidget()
        self.listB=QtGui.QListWidget()

        self.subLayoutA=QtGui.QVBoxLayout()        
        self.listLayout.insertLayout(0,self.subLayoutA)
        self.subLayoutA.addWidget(QtGui.QLabel('Label A') )
        self.subLayoutA.addWidget(self.listA)

        self.subLayoutB=QtGui.QVBoxLayout()
        self.listLayout.insertLayout(1,self.subLayoutB)
        self.subLayoutB.addWidget(QtGui.QLabel('Label B') )
        self.subLayoutB.addWidget(self.listB)


        self.layout.addWidget(self.listGroupbox) 

        self.okButton = QtGui.QPushButton('OK')
        self.okButton.clicked.connect(self.OK) 
        self.layout.addWidget(self.okButton)                      
        self.mainLayout.addWidget(self.groupbox)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def OK(self):
        print 'Ok'    
if __name__ == '__main__':
    MyApp()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top