Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidgetItem stylesheet control limitation



  • I'm trying to add a white triangle in the selected row of a QTableWidget to the last table item.
    currently i'm achieving this by setting a style sheet:

    QTableView::item:selected
    {
        color: rgb(222, 105, 89);
        background: rgb(43, 43, 43);
        outline: none;
        background-image:url('ui/table_item_selected.png');
        background-repeat: no-repeat;
        background-position: center right;
    }
    

    Unfortunately, this also adds the png file to the other items in the selected row.

    I've tried to add a dynamic property to the last item using setProperty(), to use another stylesheet setting, but i get a:

    AttributeError: 'PySide2.QtWidgets.QTableWidgetItem' object has no attribute 'setProperty'
    

    I've also tried to use

    image = QtGui.QPixmap('ui/main_nav_selected.png')
    tb_item.setBackground(QtGui.QBrush(image))
    

    but that repeats the image on the last cell and seems to conflicts with my style sheet for the selected row (as i'm using background: rgb(43, 43, 43);)

    How could I isolate the last cell item and add the png only when the row is selected?

    Any help greatly appreciated

    qtablewidget.jpg



  • @alom
    setProperty is a method of QObject, https://doc.qt.io/qt-5/qobject.html#setProperty. QTableWidgetItem is not a QObject, so you can't use it on that.


  • Banned

    Well one of the things you might try is to add another column for holding that image and then put an image place holder within that is the same size as your image but is the same color as your background -- and then swap the image out with your special image which also has the same color background. To me that seems like the easiest solution.



  • @JonB Thanks that makes sense

    @Denni-0 I'm not sure I follow you.
    How can we explicitly set the image swap on only the last cell? Keep in mind the table is expandable so i cant make an image the size of the row.

    I saw that there is a setIcon() on the QTablweWidgetItem that places the image not tiled, but i cant seem to find a way to align it to the right :/

    I have a working hacky solution atm... I added a new column on the right and replaced the item for a QLable. That way I can set a dynamic property on it. I then have a callback every time currentCellChanged() to manage the current selection and update the dynamic property of each rows QLable. I have to load the style sheet every table change though.... Any better solutions??

    QTableHack.jpg


Log in to reply