0

Using the QTDesigner I made a simple QListWidge that contains three items

Apple

Banana

Orange

My Goal, when one of those options is selected it launches a specific function. For some reason I cannot figure this out and I can't find anything within my Google searches. I am new to pyQT so maybe I'm just using the incorrect terms.

Using QT Designer I can set the SIGNAL and SLOT, but the effect is for every single item within the QListWidget, it is not specific.

Here is the code I am concerned with

QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL(_fromUtf8("itemClicked(QListWidgetItem*)")), MainWindow.close)

That code is closing the main window when any QListWidgetItem is selected. I want it to only close when "apple" is selected. I want Banana and Orange to do something else.

Seems like all the examples I find online are not addressing if you want item A to do something vs item B. They all just use a generic sample in which all items do the same thing.

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

    # Form implementation generated from reading ui file 'untitled.ui'
    #
    # Created by: PyQt4 UI code generator 4.11.4
    #
    # WARNING! All changes made in this file will be lost!

    from PyQt4 import QtCore, QtGuii

    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s

    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)

    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName(_fromUtf8("MainWindow"))
            MainWindow.resize(800, 600)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
            self.listWidget = QtGui.QListWidget(self.centralwidget)
            self.listWidget.setGeometry(QtCore.QRect(50, 60, 256, 241))
            self.listWidget.setObjectName(_fromUtf8("listWidget"))
            item = QtGui.QListWidgetItem()
            self.listWidget.addItem(item)
            item = QtGui.QListWidgetItem()
            self.listWidget.addItem(item)
            item = QtGui.QListWidgetItem()
            self.listWidget.addItem(item)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.menubar.setObjectName(_fromUtf8("menubar"))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName(_fromUtf8("statusbar"))
            MainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MainWindow)
            QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL(_fromUtf8("itemClicked(QListWidgetItem*)")), MainWindow.close)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)

        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
            __sortingEnabled = self.listWidget.isSortingEnabled()
            self.listWidget.setSortingEnabled(False)
            item = self.listWidget.item(0)
            item.setText(_translate("MainWindow", "apple", None))
            item = self.listWidget.item(1)
            item.setText(_translate("MainWindow", "banana", None))
            item = self.listWidget.item(2)
            item.setText(_translate("MainWindow", "orange", None))
            self.listWidget.setSortingEnabled(__sortingEnabled)


    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
Thomas
  • 31
  • 4

1 Answers1

1

Was able to piece together the solution using the website below and some example scripts floating around. They key to the solution was the .item

.item(0) is reviewing what is currently set at index 0. The website below showed that I could attach a .isSelected() that would return True when a list item is selected.

With both pieces of information I can now take a specific action when a specific item is selected.

http://doc.qt.io/qt-4.8/qlistwidgetitem.html#isSelected

 QtCore.QObject.connect(self.listWidget,    QtCore.SIGNAL(_fromUtf8("itemClicked(QListWidgetItem*)")), self.closewindow)

 def closewindow(self):
   apple = self.listWidget.item(0).isSelected()
   banana = self.listWidget.item(1).isSelected()

   if apple == True:
     self.exitprogram()
   elif banana == True:
     print 'Banana selected'

 def exitprogram(self):
   sys.exit()
Thomas
  • 31
  • 4