当以 PopupCompletion 模式显示弹出窗口时,我没有设法选择完成器的第一项。
我当前的代码不起作用:
completer->setCompletionPrefix(text);
QItemSelectionModel* sm = new QItemSelectionModel(completer->completionModel());
sm->select(completer->completionModel()->index(0,0), QItemSelectionModel::Select);
completer->popup()->setSelectionModel(sm);
有什么建议?
最佳答案
我会尝试更改最后两行的顺序:
completer->popup()->setSelectionModel(sm);
sm->select(completer->completionModel()->index(0,0), QItemSelectionModel::Select);
当发出 selectionChanged() 时,可能会更改弹出窗口(其 View )的选择。
所以你必须先设置选择模型,然后再进行选择。
void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command ) [virtual slot]
Selects the model item index using the specified command, and emits selectionChanged().
顺便说一句,你不必创建一个新的选择模型,只需向弹出窗口询问它(再次,它是一个 View ):
完成者->弹出()->选择模型();
http://qt-project.org/doc/qt-5.0/qtwidgets/qabstractitemview.html#selectionModel
关于qt - 在 PopupCompletion 模式下自动选择 QCompleter 的第一个命题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782277/