pixmap and text alignment in IconView



  • alt text

    I have used QListView to show thumbnails. And set the stylesheet to adjust icons.

        QListView* iconView=new QListView;
        iconView->setViewMode(QListView::IconMode);
        iconView->setUniformItemSizes(true);
        iconView->setStyleSheet("QListView::item:deselected { border: 1px solid black; border-radius: 6px; color: black;} QListView::item:selected {background: white; border: 4px solid blue;}");
    

    As you can see, vertically the pixmap aligns to top of border and the text aligns to middle of rest space. You can see it clearly in the second icon. But I want the other way around.
    Can anyone tell me how I can change the alignment of pixmap and text so that first, the text is positioned to the bottom of border, and then the pixmap algins at the middle of the rest space?


  • Moderators

    @samdol You can adjust the text using stylesheet as follows:

    "QListView::text{"
    "bottom: -4px;" //negative values
    "}"
    


  • @p3c0
    I tried but the pixmap takes the top part of icon and the text still aligned middle of the rest of space.


  • Moderators

    @samdol AFAIK these are the only ways to adjust the icon and text in listview items using stylesheets.

    QListView::text
    QListView::icon
    

    You can try setting padding-left, right, bottom and top or just left, right, top and bottom for adjusting their positions.
    More info here:
    http://doc.qt.io/qt-5/stylesheet-reference.html

    But in the end the best way is to create a custom delegate using QStyledItemDelegate.



  • @p3c0
    I have tried
    QListView::text{bottom: -4px;}
    But it lowers down the text -4px from the center line of the rest space after icon. Thus if icons have different heights, meaning different reft over space after the icons, texts after them are not aligned horizontally. Some text is higher than other texts. I would like to have all the texts to be aligned at the bottom of the border.


  • Moderators

    @samdol Well then you are only left with using QStyledItemDelegate.



  • Hi Sam,

    According to the docs,

    iconView->setUniformItemSizes(true);
    

    may only be set if items are indeed the same size.

    No idea if it will work, but try setting the gridSize instead...


Log in to reply
 

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