Have a single selection of item between 2 QListWidgets
Hi all, I am pretty new to PyQt but nevertheless currently I am encountering this problem, and I hope someone can help me.
I am trying to create a situation where there are 2 QListWidgets, List01 and List02 for example, and they contains the following.
List01 = [T01, T02, T03]
List02 = [P01, P02, P03]
I wanted the user to select an item (T01) in List01, and hence in List02, no selection (highlighting) of any items will be conducted, meaning to say if the user hovers over to List02 and selects an item (P02), the selection in List01 will be gone and it will be the item (P02) selected in List02.
Currently, I am getting the problem, where my program is able to select an item in the 2 lists.
Could someone kindly guide me?
Many thanks in advance
Re: Have a single selection of item between 2 QListWidgets
You react to the selection change signals of both lists and clear the selection of the other.
Cheers,
_
Re: Have a single selection of item between 2 QListWidgets
Quote:
Originally Posted by
anda_skoa
You react to the selection change signals of both lists and clear the selection of the other.
Cheers,
_
Hi anda, could you kindly further elaborate on that? I can somewhat understand the latter part but the first part of the reacting to change signals, could you kindly state a simple example for me?
Sorry about that...
Re: Have a single selection of item between 2 QListWidgets
QListWidget has a signal called itemSelectionChanged().
Create two slots, one that is connected to the first list's signal and one that is connected to the second list's signal.
When the slots are invoked, they clear the other list's selection.
Since clearing the selection might also result in the signal being emitted, you need to take care of that, e.g. by setting a flag and ignoring the slot invokation while it is set. In both slots obviously.
Cheers,
_
Re: Have a single selection of item between 2 QListWidgets
Quote:
Originally Posted by
anda_skoa
QListWidget has a signal called itemSelectionChanged().
Create two slots, one that is connected to the first list's signal and one that is connected to the second list's signal.
When the slots are invoked, they clear the other list's selection.
Since clearing the selection might also result in the signal being emitted, you need to take care of that, e.g. by setting a flag and ignoring the slot invokation while it is set. In both slots obviously.
Cheers,
_
Okay I will try out your method and see how it goes...
Nonetheless, I currently have a code below, it seems to be working for me, but could I get any advices if it is good enough?
Code:
from PyQt4.QtGui import *
widget.setLayout(l)
list01.addItems(["1","2","3"])
list02.addItems(["4","5","6"])
def test01():
list02.itemSelectionChanged.disconnect(test02)
for item in list02.selectedItems():
list02.setItemSelected(item,False)
list02.itemSelectionChanged.connect(test02)
def test02():
list01.itemSelectionChanged.disconnect(test01)
for item in list01.selectedItems():
list01.setItemSelected(item,False)
list01.itemSelectionChanged.connect(test01)
print dir(list01.itemSelectionChanged)
list01.itemSelectionChanged.connect(test01)
list02.itemSelectionChanged.connect(test02)
Re: Have a single selection of item between 2 QListWidgets
Yes, that looks ok.
Alternatively to the loop you could also try
Code:
list01.selectionModel().clear()
respectively for list02
Cheers,
_