在2个列表中启用单个选择

时间:2014-01-27 14:43:55

标签: python pyqt4 qlistwidget

我遇到了这个问题,我希望有人可以帮助我。

我正在尝试创建一个有2个QListWidgets的情况,例如List01List02,它们包含以下内容。

List01 = [T01, T02, T03]
List02 = [P01, P02, P03]

我希望用户在(T01)中选择一个项目List01,因此在List02中,不会对任何项目进行任何选择(突出显示),这意味着如果用户将鼠标悬停至List02并选择一个项(P02)List01中的选项将会消失,而(P02)中选择的项目List02将会消失。

目前,我遇到了问题,我的程序可以在2个列表中选择一个项目,我不知道如何执行上述操作。

有人可以指导我吗? 非常感谢提前

3 个答案:

答案 0 :(得分:0)

这里有一个示例代码,说明你如何做你想做的事情,这是非常基本的,但你可以在函数fg中得到这个想法,希望它有效:

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_()

答案 1 :(得分:0)

itemSelectionChanged()信号从一个QListWidget连接到另一个clearSelection广告位。

使用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_())

答案 2 :(得分:0)

您可以举例说明此代码:

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_())