2011-04-19 147 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

相关问题