2016-10-09 103 views

回答

1

這可以很容易地用一個簡單的自定義項目,委託來完成:

class ItemDelegate(QtGui.QStyledItemDelegate): 
    def paint(self, painter, option, index): 
     option.decorationPosition = QtGui.QStyleOptionViewItem.Right 
     super(ItemDelegate, self).paint(painter, option, index) 

class MainWindow(QtGui.QMainWindow, Ui_MainWindow): 
    def __init__(self): 
     ... 
     self.delegate = ItemDelegate() 
     self.listWidget.setItemDelegate(self.delegate) 

要從項目中刪除的圖標,只需將其設置爲空QIcon

listItem.setIcon(QtGui.QIcon()) 
+0

工程就像一個魅力!感謝您的徹底解答! – Yasin

1

爲此,您需要提供您自己的物品委託,以便像您想要的那樣繪製物料數據。

請參閱QAbstractItemView::setItemDelegate()

您可能會使用QStyledItemDelegate作爲您的基類,並讓其paint()處理除Qt::DecorationRole(圖標)以外的所有方面並自行繪製。

+0

我明白了。感謝你的回答。我之前沒有創建自定義委託,所以我不知道如何在PyQt中實現它,有沒有像我可以參考的參考? – Yasin

+0

我自己並沒有使用PyQt,但我的猜測是你使用標準的Python子類化語法,然後用一個函數簽名像'def paint(self,painter,option,index)'覆蓋paint方法:' –

+0

我認爲我找到了一個很好的例子。 http://www.saltycrane.com/blog/2008/01/pyqt4-qitemdelegate-example-with/但事情是我的列表小部件是在Qt設計器中創建的,我不知道如何無縫地將其替換爲委託版本! – Yasin