Это можно сделать, если вы готовы принять бессовестный взлом :-)
Предполагая следующий код...
QFileDialog fd;
Проверка fd
с использованием fd.findChildren<QListView *>()
показывает, что у него есть два дочерних элемента, которые либо являются наследниками QListView
...
- QListView с именем listView
- QSidebar названная боковая панель
(где QSidebar является частным для Qt).
Работая в предположении, что QListView
с именем listView
представляет собой интересующий вас виджет, вы можете подключить обратный вызов к его модели выбора...
QFileDialog fd;
for (const auto &i: fd.findChildren<QListView *>("listView")) {
auto *sm = i->selectionModel();
QObject::connect(sm, &QItemSelectionModel::selectionChanged,
[sm](const QItemSelection &selected, const QItemSelection &deselected)
{
/*
* Here we pass a hard-coded max selected items
* value of 5 to the real callback/slot.
*/
handle_selection_updated(5, sm, selected, deselected);
});
}
Здесь handle_selection_updated
имеет следующее определение...
void handle_selection_updated (int selection_max, QItemSelectionModel *sm,
const QItemSelection &selected,
const QItemSelection &deselected)
{
/*
* We need to remember the last valid selection. The following
* is declared static in this simple case but would generally be
* a class member in `real' code.
*/
static QItemSelection last_selected;
/*
* Because we update the selection model `sm' from within this
* slot this function will recurse which will cause problems if
* we don't detect it and take appropriate action.
*/
static bool recursing = false;
if (recursing)
return;
/*
* If the number of rows selected is greater than the value
* specified by `selection_max' then revert to the last valid
* selection as specified by `last_selected'.
*/
if (sm->selectedRows().size() > selection_max) {
/*
* The following call to QItemSelectionModel::clearSelection
* will result in a recursive call to this function. Set
* `recursing' to true to catch this and avoid associated
* problems.
*/
recursing = true;
/*
* Now clear the selection and reset it to the items from
* `last_selected'.
*/
sm->clearSelection();
for (const auto &i: last_selected.indexes()) {
sm->select(i, QItemSelectionModel::Select);
}
recursing = false;
}
/*
* Update `last_selected'.
*/
last_selected = sm->selection();
}
Я только кратко протестировал приведенный выше код, но, похоже, он ведет себя так, как требуется.
person
G.M.
schedule
18.06.2020
selectFile(const QString& filename)
? Возможно, это тот, кого вы ищете. - person rbaleksandar   schedule 17.06.2020