Custom widget not appearing in QListWidget/QListWidgetItem



  • Hi All

    I'm using pyQt 5 and trying to display a custom widget within a standard QListWidgetItem, in a standard QListWidget. The code runs but nothing appears. The code isnt fancy;

    def addNewSourceFolder(self):
        StartFolder = "/home"
        DirName = QtWidgets.QFileDialog.getExistingDirectory(self, "Choose Source Folder", expanduser("~"), QtWidgets.QFileDialog.ShowDirsOnly) 
        if DirName:
            widgetitems = []
            widgetitems = self.ui.source_directory_view.findItems(DirName, QtCore.Qt.MatchExactly)
            if len(widgetitems) == 0:
                item = QtWidgets.QListWidgetItem()
    
                picwidget = ShootWidgetItem()
                picwidget.setTextName(DirName)
                picwidget.setTextPath(DirName)
                picwidget.setWidgetIcon("folder_pic.png")
                self.ui.source_directory_view.addItem(item)
                self.ui.source_directory_view.setItemWidget(item, picwidget)
    
            widgetitems.clear()
    

    The custom widget is as follows;

    class ShootWidgetItem(QtWidgets.QWidget):
    
          def __init__(self, parent=None):
            super(ShootWidgetItem, self).__init__(parent)
            self.data = []
            self.filepath = ""
            self.textQVBoxLayout = QtWidgets.QVBoxLayout()
            self.textNameLabel    = QtWidgets.QLabel()
            self.textPathLabel  = QtWidgets.QLabel()
            self.textQVBoxLayout.addWidget(self.textNameLabel)
            self.textQVBoxLayout.addWidget(self.textPathLabel)
            self.allQHBoxLayout  = QtWidgets.QHBoxLayout()
            self.iconQLabel      = QtWidgets.QLabel()
            self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
            self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
            self.setLayout(self.allQHBoxLayout)
            # setStyleSheet
            self.textNameLabel.setStyleSheet('''
                color: rgb(0, 0, 255);
            ''')
            self.textPathLabel.setStyleSheet('''
                color: rgb(255, 0, 0);
            ''')
    
          def setTextName (self, text):
            self.textNameLabel.setText(text)
    
          def setTextPath (self, text):
            self.textPathLabel.setText(text)
    
          def setWidgetIcon (self, imagePath):
            self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
    
          def getTextName(self):
              return self.textNameLabel.text()
    

    Anyone have any ideas why this is not appearing in the QlistWidget?


  • Lifetime Qt Champion

    Hi,

    Might be a silly idea but did you try to show picwidget once ? Just to be sure it's not empty for whatever reason.



  • @SGaist Thanks for the reply! Not sure why, but this line in addNewSourceFolder did it;

    item.setSizeHint(picwidget.sizeHint())
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.