开发者

QListView height according to contents

开发者 https://www.devze.com 2023-02-26 20:35 出处:网络
I开发者_运维技巧 have a QListView with a list of strings. Basically, I use it as pop window for a QLineEdit for an autocomplete process.

I开发者_运维技巧 have a QListView with a list of strings.

Basically, I use it as pop window for a QLineEdit for an autocomplete process. I dont want the QListView to display blank lines, only lines that have strings in it. See this:

QListView height according to contents

I want it to resize itself automatically so it wont have these blank rows after the last entry.

Thanks


You can try to do this: Reimplement QListView::rowsInserted() method class. Say you have MyListView inherited from QListView. So the code can look like this:

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()); 
}

But to do it simpler, I'd recommend you to use QCompleter class with QLineEdit. Its already designed for what you need and you don't need to spend time trying to make it work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号