Whenever I click any function in the list widget, it ran a specific function. Now I want to send the item itself, as a parameter to that function. Here is the code:
QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("itemClicked(QListWidgetItem *)"), self.test)
def test(self):
print 'hello'
Instead I want:
def test(self,item):
print item
That should already work - when itemClicked
is emitted it sends the clicked QListWidgetItem
as a parameter. You just need to edit your test
function to accept an extra parameter, and that will be your QListWidgetItem
.
from PyQt4.QtCore import QCoreApplication, Qt
from PyQt4.QtGui import QListWidget, QListWidgetItem, QApplication
import sys
class MyList(QListWidget):
def __init__(self):
QListWidget.__init__(self)
self.add_items()
self.itemClicked.connect(self.item_click)
def add_items(self):
for item_text in ['item1', 'item2', 'item3']:
item = QListWidgetItem(item_text)
self.addItem(item)
def item_click(self, item):
print item, str(item.text())
if __name__ == '__main__':
app = QApplication([])
myList = MyList()
myList.show()
sys.exit(app.exec_())
In the above example you'll see a QListWidget with three items. When you click an item the item along with the item's text will be printed to the console.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With