2013-04-28 94 views
1

我想激活從listWidget一個項目,那就是ChildWidget的一部分,在使用parentWidget事件過濾器。下面是相關parentWidget部分代碼:PyQt的:激活QListWidget項目

self.w = ChildWidget() 
    def eventFilter(self, source, event): 
     if event.type() in (QtCore.QEvent.MouseButtonPress, 
         QtCore.QEvent.MouseButtonDblClick): 
     if event.button() == QtCore.Qt.LeftButton: 
       self.w.listWidget.itemActivated 

和ChildWidget:

self.listWidget.itemActivated.connect(self.klik) 
    if item.data(Qt.UserRole).toPyObject(): 
     mp3=item.data(Qt.UserRole).toPyObject() 
     playsnd("/home/pi/Desktop/komunikator/Recenice/pekara/"+mp3) 

事件過濾器工作正常,只是

self.w.listWidget.itemActivated 

不工作的命令。是否有其他方式激活項目和觸發項目激活的信號發送?

回答

2
In [9]: q.itemActivated 
Out[9]: <bound signal itemActivated of QListWidget object at 0x1d13560> 

你不叫它。

但是,讓我們嘗試調用它。

In [10]: q.itemActivated() 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-10-3602f33f377a> in <module>() 
----> 1 q.itemActivated() 

TypeError: native Qt signal is not callable 

你不能調用它,你必須直接調用綁定過程。要發出信號,您必須使用發射。

q.itemActivated.emit(None) 
+0

好了,可以請你解釋什麼,應該怎麼辦?我試過self.w.listWidget.itemActivated.emit(無)。它發送信號,但函數klik沒有收到參數「item」。我還去哪裏? – speedyTeh 2013-04-28 21:41:02

+0

因爲你寫了「無」作爲參數......把合適的人。 – LtWorf 2013-04-28 22:13:42

+0

我做到了!的行是:self.w.listWidget.itemActivated.emit(self.w.listWidget.currentItem()) – speedyTeh 2013-04-28 23:11:11