Home > OS >  python pyqt5 getting output every time i change the qlistwidget index
python pyqt5 getting output every time i change the qlistwidget index

Time:10-12

so basically I have 3 list widgets and the problem is that when I select a row in one list and I select another row in one of the other lists both of the lists have indexes above -1 (meaning they are both selected). what I want them to do is that when I press another row on a different list, the first lists index to change back to -1 making the row from that list unselected. in order to do that I need to get input every time I select a row from any list unless there are other options to solve this problem. hope that wasn't too complicated to understand. any help or attempt to help will be much appreciated. here is a picture of the problem:

1

CodePudding user response:

You could subclass QListWidget and have it emit a custom signal when the selection changes that emits the instance of the the widget as the parameter.

Then in the parent widget you could have a single slot that would deselect the other two list widgets that works for all three lists.


update

Based on the information provided in the comments, you actually don't need to subclass QListWidget, instead you can get the instance using the currentItemChanged signal since it emits the previously selected item and the item it has changed to as arguments. Then using the QListWidgetItem.listWidget() method you can get the instance of the list that emitted the signal.

Here is an example:

class Window(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.layout = QHBoxLayout(self)
        self.resize(500,500)
        self.lists = [QListWidget(parent=self) for _ in range(3)]
        for widget in self.lists:
            self.layout.addWidget(widget)
            for i in range(30):
                item = QListWidgetItem(type=0)
                item.setText(str(i))
                widget.insertItem(i, item)
            widget.currentItemChanged.connect(self.deselect)

    def deselect(self, new, old):
        if not new: return
        instance = new.listWidget()
        for widget in self.lists:
            if widget != instance:
                widget.setCurrentRow(-1)

CodePudding user response:

I think you need to connect to the currentRowChanged signal of QListWidget.

If your window layout is purely in code, then connecting the signals will look like this:

self.listwidget1.currentRowChanged.connect(self.on_listwidget1_currentRowChanged)

self.listwidget2.currentRowChanged.connect(self.on_listwidget2_currentRowChanged) self.listwidget2.currentRowChanged.connect(self.on_listwidget3_currentRowChanged)

If you are using Qt Designer, then the connections are automatically done by pyuic. Then your methods for handling changes in your QListWidgets are:

def on_listwidget1_currentRowChanged(self, currentRow):
    <put your code here>

def on_listwidget2_currentRowChanged(self, currentRow):
    <put your code here>

def on_listwidget3_currentRowChanged(self, currentRow):
    <put your code here>
  • Related