例子
以下示例显示了捕获单击事件以弹出消息框。
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class myListWidget(QListWidget):
def Clicked(self,item):
QMessageBox.information(self, "ListWidget", "You clicked: "+item.text())
def main():
app = QApplication(sys.argv)
listWidget = myListWidget()
#Resize width and height
listWidget.resize(300,120)
listWidget.addItem("Item 1");
listWidget.addItem("Item 2");
listWidget.addItem("Item 3");
listWidget.addItem("Item 4");
listWidget.setWindowTitle('PyQT QListwidget Demo')
listWidget.itemClicked.connect(listWidget.Clicked)
listWidget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
上面的代码产生以下输出。状态栏显示所选菜单按钮的标题 -