qt - 在 PopupCompletion 模式下自动选择 QCompleter 的第一个命题

标签 qt select selection qcompleter

当以 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/

相关文章:

c++ - QT中如何将带逗号的字符串转换为 float ?

c++ - A 内的 Gumbo HTML 文本

SQL Server如何按顺序选择第一行

javascript - 在不考虑其直接父级的情况下获取开始和结束偏移值的范围

javascript - 范围内的 SurroundContents 跨越多个标签(getRangeAt 问题?)

c++ - 循环 : future iterations overwrite results of previous iterations 中的 If-else 条件

c++ - 有没有更简单的方法从 QList<Class*> 转换为 QList<QPointer<Class>>?

sql - 内连接和 where in select join SQL 语句的区别

mysql - 从mysql中的大表中快速选择随机行

c++ - 从 gtk treeview 的选定行获取数据 - gtkmm,c++