how to add image with text in qlistwidget pyqt4 python?
Question: How to add image/icon with text in a qlistwidget in pyqt4 python? I want to
add an icon with text just like a chat system. thanks


Answer: I have tried this right now and it works, supposing you have a file named
tick.png in the same folder as this script.

    
    
    import sys
    from PyQt4 import QtGui, QtCore
    from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        window = QDialog()
    
        list = QListWidget( window )
    
        itm = QListWidgetItem( "Tick" );
        itm.setIcon(QIcon(r"tick.png"));
        list.addItem(itm);
    
        window.show( )
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

The chat-like-icon system may be different from this, but right now I don't
see a way to have a QListWidgetItem with multiple smileys and text.

You may think of smileys as a particular case of a QListWidgetItem where the
text is blank and only the icon is present.

Another solution is using a read-only QTextEdit as chatboard and have the user
typing its text + icon + text (etc.) in a separate editable QTextEdit. Then,
when he presses the send button, append everything he typed to the read-only
QTextEdit.

    
    
    import sys
    from PyQt4 import QtGui, QtCore
    from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon, QTextEdit, QTextDocumentFragment
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        window = QDialog()
    
        list = QListWidget( window )
    
        textEditor = QTextEdit( window );
        textEditor.setReadOnly( True )
        tick_icon = QTextDocumentFragment.fromHtml(r"<img src='tick.png'>");
    
        textEditor.insertPlainText ( " ValiumKnight writes: " )
        textEditor.textCursor().insertFragment(tick_icon);
        textEditor.insertPlainText ( " Hello World " )
        textEditor.textCursor().insertFragment(tick_icon);
        textEditor.textCursor().insertFragment(tick_icon);
        textEditor.textCursor().insertFragment(tick_icon);
    
        window.show( )
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

Bye!

