Question

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 and I am not sure how to perform the above.

Could someone kindly guide me? Many thanks in advance

Was it helpful?

Solution

OK here is an example code of how could you do what you want, it's very basic but you can get the idea within the functions f and g, hope it works:

import PyQt4.QtGui as gui


app = gui.QApplication([]) 


w = gui.QWidget()
l = gui.QHBoxLayout(w)
w.setLayout(l)

lis1 = gui.QListWidget()
lis2 = gui.QListWidget()

lis1.addItems(["1","2","3"])
lis2.addItems(["4","5","6"])

def f():    
    lis2.itemSelectionChanged.disconnect(g)    
    for item in lis2.selectedItems():
        lis2.setItemSelected(item,False)
    lis2.itemSelectionChanged.connect(g)


def g():
    lis1.itemSelectionChanged.disconnect(f)    
    for item in lis1.selectedItems():
        lis1.setItemSelected(item,False)    
    lis1.itemSelectionChanged.connect(f)


print dir(lis1.itemSelectionChanged)

lis1.itemSelectionChanged.connect(f)
lis2.itemSelectionChanged.connect(g)

l.addWidget(lis1)
l.addWidget(lis2)

w.show()


app.exec_()

OTHER TIPS

Connect the itemSelectionChanged() signal from one QListWidget to the clearSelection slot of the other.

Example generated with QtDesigner:

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(214, 158)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.listWidget = QtGui.QListWidget(Form)
        self.listWidget.setObjectName("listWidget")
        item = QtGui.QListWidgetItem()
        item.setText("T01")
        self.listWidget.addItem(item)
        item = QtGui.QListWidgetItem()
        item.setText("T02")
        self.listWidget.addItem(item)
        item = QtGui.QListWidgetItem()
        item.setText("T03")
        self.listWidget.addItem(item)
        self.gridLayout.addWidget(self.listWidget, 0, 0, 1, 1)
        self.listWidget_2 = QtGui.QListWidget(Form)
        self.listWidget_2.setObjectName("listWidget_2")
        item = QtGui.QListWidgetItem()
        item.setText("P01")
        self.listWidget_2.addItem(item)
        item = QtGui.QListWidgetItem()
        item.setText("P02")
        self.listWidget_2.addItem(item)
        item = QtGui.QListWidgetItem()
        item.setText("P03")
        self.listWidget_2.addItem(item)
        self.gridLayout.addWidget(self.listWidget_2, 0, 1, 1, 1)

        # This are the important lines.
        QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("itemSelectionChanged()"), self.listWidget_2.clearSelection)
        QtCore.QObject.connect(self.listWidget_2, QtCore.SIGNAL("itemSelectionChanged()"), self.listWidget.clearSelection)
        QtCore.QMetaObject.connectSlotsByName(Form)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

You can take example on this code:

from PyQt4 import QtCore, QtGui
import sys

app = QtGui.QApplication(sys.argv)

class MyApp(QtGui.QDialog):
  def __init__(self):
    super(MyApp, self).__init__()

    layout = QtGui.QHBoxLayout()

    qlist1 = QtGui.QListWidget()
    qlist1.addItems(["elem1","elem2","elem3"])
    layout.addWidget(qlist1)

    qlist2 = QtGui.QListWidget()
    qlist2.addItems(["elem4","elem5","elem6"])
    layout.addWidget(qlist2)

    # This dict will be used when a list is clicked
    # to clear the selection of the other list
    self.list_dict = {}
    self.list_dict[qlist1] = qlist2
    self.list_dict[qlist2] = qlist1

    qlist1.clicked.connect(self.list_clicked)
    qlist2.clicked.connect(self.list_clicked)

    self.setLayout(layout)
    self.show()

  def list_clicked(self):
    self.list_dict[self.sender()].clearSelection()

myApp = MyApp()
sys.exit(app.exec_())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top