pythonpyqtpyqt4qlistwidgetitem

Call a Function with clicked Item from QListWidgetItem in Python


I am using Qt-designer and PyQt. I set up a Window with some Buttons and a QListWidget.

I want to get the clicked item of a QListWidget. For example I have 4 Items in the List and I click the first one the item should be sent to another function.

I read some examples but they always use classes and "self". Since I am a total newbie I want to start simple and set everything up without a class.

def show_item(item):
    print (item)

mainscreen.connect(mainscreen.myList, SIGNAL("itemClicked(QListWidgetItem*)"),QListWidgetItem, showItem(QListWidgetItem))

I don't really understand those errors since I thought all of the requirements are fullfilled.

TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'PyQt4.QtCore.pyqtWrapperType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 4 has unexpected type 'str'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'PyQt4.QtCore.pyqtWrapperType'

Edit: Code Sample

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import 

def show_item(item):
    print (item)



for x in range(4):
    mainscreen.mylist_sorted.addItem(x)

app = QApplication(sys.argv)
mainscreen = loadUi("mainscreen.ui")
mainscreen.connect(mainscreen.mylist_sorted, SIGNAL("itemClicked(QListWidgetItem*)"),QListWidgetItem, (showItem(QListWidgetItem)))
mainscreen.show()
sys.exit(app.exec_())

mainscreen.ui file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>mainscreen</class>
 <widget class="QMainWindow" name="mainscreen">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QListWidget" name="mylist_sorted">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>160</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Solution

  • Your code has the following errors:

    Considering the above the solution is:

    import sys
    from PyQt4.QtCore import SIGNAL
    from PyQt4.QtGui import QApplication, QListWidgetItem
    from PyQt4.uic import loadUi
    
    
    def show_item(item):
        print(item)
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        mainscreen = loadUi("mainscreen.ui")
        for x in range(4):
            mainscreen.mylist_sorted.addItem(str(x))
    
        mainscreen.connect(
            mainscreen.mylist_sorted,
            SIGNAL("itemClicked(QListWidgetItem*)"),
            show_item,
        )
        mainscreen.show()
        sys.exit(app.exec_())
    

    Although it is advisable to use the new connection syntax:

    import sys
    from PyQt4.QtCore import SIGNAL
    from PyQt4.QtGui import QApplication, QListWidgetItem
    from PyQt4.uic import loadUi
    
    
    def show_item(item):
        print(item)
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        mainscreen = loadUi("mainscreen.ui")
        for x in range(4):
            mainscreen.mylist_sorted.addItem(str(x))
    
        mainscreen.mylist_sorted.itemClicked.connect(show_item)
        mainscreen.show()
        sys.exit(app.exec_())