PDA

View Full Version : Have a single selection of item between 2 QListWidgets



salik89
27th January 2014, 13:09
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

anda_skoa
27th January 2014, 13:52
You react to the selection change signals of both lists and clear the selection of the other.

Cheers,
_

salik89
27th January 2014, 13:56
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...

anda_skoa
27th January 2014, 19:24
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,
_

salik89
29th January 2014, 14:34
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?


from PyQt4.QtGui import *
app = QApplication([])
widget = QWidget()
mainLayout = QHBoxLayout(widget)
widget.setLayout(l)

list01 = QListWidget()
list02 = QListWidget()

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)

anda_skoa
29th January 2014, 18:53
Yes, that looks ok.

Alternatively to the loop you could also try



list01.selectionModel().clear()

respectively for list02

Cheers,
_