Skip to content

QComboBox

1 隐藏 item

QComboBox *editor = new QComboBox(parent);
editor->addItems(item_list_);
editor->installEventFilter(const_cast<ConeComboxDelegate *>(this));

const auto column = index.column();

if (2 == column) {
    const auto cone_data = index.model()->data(index, Qt::UserRole).toInt();

    const auto data = ConeTabelModel::GetConePartName(cone_data);
    const auto part_size = std::get<2>(data);
    const auto cone_id = std::get<3>(data);

    QListView *view = qobject_cast<QListView *>(editor->view());
    Q_ASSERT(view != nullptr);

    for (int i = 0; i < item_list_.size(); i++) {
        if (i < part_size + cone_id && i >= cone_id) {
            view->setRowHidden(i, false);
        } else {
            view->setRowHidden(i, true);
        }
    }

    view->setFixedHeight(25 * part_size);
}

参考: how-to-hide-qcombobox-items-instead-of-clearing-them-out