2011-04-19 141 views
3

我有一個QListView的字符串列表。根據內容的QListView高度

基本上,我使用它作爲自動完成過程的QLineEdit的彈出窗口。 我不希望QListView顯示空行,只有行中有字符串的行。 看到這個: Image

我希望它自動調整自己的大小,所以它不會有最後一次輸入後的空白行。

感謝

+1

你看過QCompleter嗎?這應該做你想做的。 – 2011-04-19 21:03:51

+0

我不想自動完成,我試圖避免有這些空行。 – snoofkin 2011-04-19 23:22:02

回答

3

你可以試着這樣做:重新實現QListView::rowsInserted()方法類。假設你有從QListView繼承的MyListView。因此,代碼可以是這樣的:

void MyListView::rowsInserted (const QModelIndex & parent, int start, int end) 
{ 
    QListView::rowsInserted(parent, start, end); 
    int rc = model()->rowCount(); 

    // use this all the rows have the same height. otherwise 
    // you will need to iterate and sum all row heights 
    resize(width(), rc ? rc*sizeHintForRow(0): height()); 
} 

但要做到這一點簡單的,我建議你使用with QLineEditQCompleter類。它已經爲你需要的設計而設計,你不需要花時間努力工作。

+0

會試一試... – snoofkin 2011-04-20 00:13:38

+0

好吧,我用'resize(width(),rc?rc * sizeHintForRow(0):height());'在paintEvent方法中,並且看起來像是有竅門。 – snoofkin 2011-04-20 12:47:35

相關問題