Search code examples
pyqtcenteralignmentqlistwidgetqlist

QListWidget Align Items Center


I am adding a list of strings to a QList Widget like this:

myList.addItems( [ 'item1' , 'item2' , 'item3' ] )

By default the list aligns these to the left but I want to get them in the center of the list.

Any Ideas?


Solution

  • If you create a QListWidgetItem() you can call its setTextAlignment() method and pass Qt.AlignHCenter:

    item_text_list = [ 'item1' , 'item2' , 'item3' ]
    
    for item_text in item_text_list:
        item = QListWidgetItem(item_text)
        item.setTextAlignment(Qt.AlignHCenter)
        myList.addItem(item) 
    

    Docs: QListWidgetItem, Qt.AlignmentFlag