简体   繁体   English

防止在修改项目列表时更改可编辑的QComboBox选择

[英]Prevent editable QComboBox selection from changing when item list modified

I have an editable QComboBox containing a list of ID numbers. 我有一个包含ID号列表的可编辑QComboBox。

The ID numbers represent devices attached to the system. ID号代表连接到系统的设备。 The devices are frequently added and removed at runtime. 设备经常在运行时添加和删除。

The intent of the editable combo box is to contain, in its list, the list of IDs currently attached, for easy selection, but at the same time allow the user to manually enter IDs of devices not currently present. 可编辑组合框的目的是在其列表中包含当前附加的ID列表,以便于选择,但同时允许用户手动输入当前不存在的设备的ID。 Also, it's valid to enter an empty string for the ID. 同样,为ID输入一个空字符串也是有效的。

Basically I want to provide the user with a way to enter an arbitrary (or no) ID, with the added bonus of a quick selection of devices currently attached (which changes at runtime). 基本上,我想为用户提供一种输入任意(或不输入)ID的方法,以及快速选择当前连接的设备(运行时会更改)的额外好处。

I am not currently using a list model to maintain the list, I'm just using the QComboBox's add and remove functions. 我目前不使用列表模型来维护列表,而只是使用QComboBox的添加和删除功能。

I need the selection to not change if devices are added or removed. 如果添加或删除设备,我需要选择不更改。 However, I'm running into the following problems: 但是,我遇到了以下问题:

  • When the currently entered ID (either manually or by list selection) is removed from the list, the selection is changed to another ID in the list. 从列表中删除当前输入的ID(手动或通过列表选择)后,选择内容将更改为列表中的另一个ID。
  • When the list is empty and an ID is entered manually, or no ID is entered at all, the selection is changed when an ID is added to the list. 当列表为空并且手动输入ID或根本不输入ID时,将ID添加到列表时,选择会更改。

Is there a way to make it so that adding and removing items from the combo box never, ever modifies the selection in the edit box? 有没有一种方法可以使从组合框中添加和删除项目永远不会修改编辑框中的选择? Or even some other UI element that accomplishes my goal? 甚至其他一些实现我目标的UI元素?

Before updating the combo box, save the currently selected ID (or the blank string) to a temp. 在更新组合框之前,将当前选择的ID(或空白字符串)保存为临时文件。 variable. 变量。 After modifying the combo box contents, check if that ID still exists in the combo box (eg with findText() ). 修改组合框内容后,检查该ID是否仍然存在于组合框中(例如,使用findText() )。 If it does, select it with setCurrentIndex() . 如果是这样,请使用setCurrentIndex()选择。 If it does not, set it with setCurrentText() or setEditText() . 如果不是,请使用setCurrentText()setEditText()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM