我有一个带有字符串列表的 QListView。

基本上,我将它用作 QLineEdit 的弹出窗口以进行自动完成过程。
我不希望 QListView 显示空行,只显示其中包含字符串的行。
看到这个:

我希望它自动调整自身大小,以便在最后一次输入后不会有这些空白行。

谢谢

最佳答案

您可以尝试这样做:重新实现 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());
}

但为了更简单,我建议您使用 QCompleterwith QLineEdit 。它已经为您的需要而设计,您无需花时间尝试使其工作。

关于c++ - QListView 高度根据内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5721692/

10-10 23:22