Search code examples
pythonpython-3.xpyqtpyqt5qlistview

setGridSize(...) and text eliding


I am using QListView with QFileSystemModel to make a file manager. I am using QListView::ListMode. The problem I am facing is despite settings a grid size, the text does not get elided. Here is the code I am using.

import sys, os

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

app = QApplication( sys.argv )

fsm = QFileSystemModel()
lv = QListView()
lv.setViewMode( QListView.ListMode )

lv.setResizeMode( QListView.Adjust );
lv.setMovement( QListView.Static );
lv.setSelectionMode( QListView.ExtendedSelection );
lv.setWrapping( True );
lv.setFlow( QListView.LeftToRight );
lv.setTextElideMode( Qt.ElideRight );
lv.setUniformItemSizes( True );

lv.setIconSize( QSize( 48, 48 ) )
lv.setGridSize( QSize( 200, 56 ) )

lv.setModel( fsm )
lv.setRootIndex( fsm.setRootPath( "/path/to/my/folder/" ) )

lv.show()

app.exec_()

What I get is this garbage:

enter image description here

I do not have a clue why this is so? In the case I do not set a grid size, then the icons and text are rendered properly. Is this some Qt bug?

I'm using Debian Sid, Qt5 5.11.3+dfsg1-1, Python3/PyQt5: 3.7.3-1/5.11.3+dfsg-1+b3 I have even tried this with Qt5/C++ and the problem shows up even there.


Solution

  • I do not get what the OP shows in the image, but I get the following:

    enter image description here

    It shows that the gridSize does not change the size of the item but the separation between them as shown by the ruler.

    To change the width of the item I have implemented a delegate:

    # ...
    class StyledItemDelegate(QStyledItemDelegate):
        def sizeHint(self, option, index):
            s = super().sizeHint(option, index)
            s.setWidth(200)
            return s
    
    # ...
    lv = QListView()
    delegate = StyledItemDelegate(lv)
    lv.setItemDelegate(delegate)
    # ...
    

    enter image description here