sebastien deepwali
sebastien deepwali

Reputation: 29

QListWidget don't show the selected row

I made a list in my program and a function to reorder (up/down) the rows So i use setCurrentRow() to change the selected row after moving it works well but the selection is not shown in the list

How can i do that

ps: i tried setSelectionRectVisible(True) but nothing happen.

import sys
from PyQt5.QtWidgets import *
class MainWindow(QWidget):

    def __init__(self,parent=None):
        super(MainWindow, self).__init__()

        self.my_list = QListWidget(self)
        self.my_list.setGeometry(5,5,90,100)
        QListWidgetItem("1",self.my_list)
        QListWidgetItem("2",self.my_list)
        QListWidgetItem("3",self.my_list)
        QListWidgetItem("4",self.my_list)

        self.upButton = QPushButton("Up",self)
        self.upButton.move(100,10)
        self.upButton.clicked.connect(self.up)
        self.downButton = QPushButton("Down",self)
        self.downButton.move(100,30)
        self.downButton.clicked.connect(self.down)

    def up(self, event):
        w=self.my_list.currentRow() 
        if w > 0:
            self.my_list.insertItem(w-1, self.my_list.currentItem().text())
            self.my_list.takeItem(w+1)
            self.my_list.setCurrentRow(w-1)
            # self.my_list.setSelectionRectVisible(True)

    def down(self, event):
        w = self.my_list.currentRow() 
        if w < self.my_list.count()-1:
            self.my_list.insertItem(w+2, self.my_list.currentItem().text())
            self.my_list.takeItem(w)
            self.my_list.setCurrentRow(w+1)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    root = MainWindow()
    root.show()
    sys.exit(app.exec_())

I was creating this simple example to show and it works well with default background (grey)

So my problem is to find the name of the highlighted property to set it

Upvotes: 0

Views: 161

Answers (1)

sebastien deepwali
sebastien deepwali

Reputation: 29

So i found a solution in the stylesheet i have to put:

 QListWidget::item:selected:!active { background: #0099cc}

thanks

Upvotes: 2

Related Questions