簡體   English   中英

調整 QListView 的高度以適應內容

[英]Adjust the height of QListView to fit the content

我想要一個基於QAbstractListModelQListView的高度來適應內容,如果項目的數量小於給定的數字 N。如果有超過 N 個項目,它應該只顯示 N 個項目。 我在網上閱讀了很多奇怪的技巧,但其中大多數看起來像黑客。 我想這與sizeHint()但在模型視圖方法中,沒有 ItemWidget 可以覆蓋sizeHint() 實現這種行為的正確方法是什么?

此外,這與父應用程序的大小策略有何關聯? 這是第二個約束:內容不應嘗試使用它們在父小部件中的空間,但父小部件應調整大小以適合QListView

這不是這個問題的重復,因為我不能使用QCompleter

sizeHint() 必須在 QListView (分別是它的子類)中被覆蓋。 提到的特殊行為可以在那里實現。 例如像這樣:

QSize ProposalListView::sizeHint() const
{
    if (model()->rowCount() == 0) return QSize(width(), 0);
    int nToShow = _nItemsToShow < model()->rowCount() ? _nItemsToShow : model()->rowCount();
    return QSize(width(), nToShow*sizeHintForRow(0));
}

這就要求項目委托的大小提示要合理。 就我而言:

inline QSize sizeHint ( const QStyleOptionViewItem&, const QModelIndex& ) const  override { return QSize(200, 48); }

現在我只需要在更改模型后調用 updateGeometry() 即可。

對於那些在 QML 中使用 ListView 和 QtQuick 控件的人。 我使內容適合屬性 anchors.bottomMargin。

anchors.bottomMargin: 20

沒有好的方法可以做到這一點。 我使用以下代碼。

標題:

class List_view_auto_height : public QObject {
  Q_OBJECT
public:
  explicit List_view_auto_height(QListView * target_list);
  void set_max_auto_height(int value);
  void set_min_height(int value);

private:
  QListView* list;
  QTimer timer;
  int _min_height;
  int _max_height;

  bool eventFilter(QObject* object, QEvent* event);

private slots:
  void update_height();
};

來源:

List_view_auto_height::List_view_auto_height(QListView *target_list) :
  QObject(target_list)
, list(target_list)
{
  _min_height = 0;
  _max_height = 250;
  connect(list->model(), &QAbstractItemModel::rowsInserted,
          this, &List_view_auto_height::update_height);
  connect(list->model(), &QAbstractItemModel::rowsRemoved,
          this, &List_view_auto_height::update_height);
  connect(list->model(), &QAbstractItemModel::layoutChanged,
          this, &List_view_auto_height::update_height);
  list->installEventFilter(this);
  update_height();
  connect(&timer, &QTimer::timeout, this, &List_view_auto_height::update_height);
  timer.start(500);
}

void List_view_auto_height::set_max_auto_height(int value) {
  _max_height = value;
  update_height();

}

void List_view_auto_height::set_min_height(int value) {
  _min_height = value;
  update_height();
}

bool List_view_auto_height::eventFilter(QObject *object, QEvent *event) {
  if (event->type() == QEvent::Show) {
    update_height();
  }
  return false;
}

void List_view_auto_height::update_height() {
  if (!list->isVisible()) { return; }
  int height = 0;
  if (list->model()->rowCount() > 0) {
    height = list->visualRect(list->model()->index(list->model()->rowCount() - 1, 0)).bottom() + 1;
    height -= list->visualRect(list->model()->index(0, 0)).top();
  }
  if (list->horizontalScrollBar()->isVisible()) {
    height += list->horizontalScrollBar()->height();
  }
  bool scrollbar_enabled = false;
  if (_max_height != 0 && height > _max_height) {
    height = _max_height;
    scrollbar_enabled = true;
  }
  if (height < _min_height) {
    height = _min_height;
  }
  list->setFixedHeight(height + 6);
}

用法:

new List_widget_auto_height(list);

它充滿了黑客,在某些情況下可能無法正常工作。 隨意改進它。

它使用setFixedHeight設置高度。 這應該為父小部件的大小提示提供正確的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM