Qt如何根据另一个ComboBox中的选定选项设置ComboBox的值

Qt How to set the values of ComboBox based on the selected option in another ComboBox

我正在尝试开发一个需要从用户那里获得各种输入的应用程序。uild。输入选项因先前选择的输入而异。 像这样:

Image

因此,如果用户选择“数字”选项,右侧的组合框应显示数值。

Image

同样,如果选择字母表,它应该显示 A、B、C 等

所以我需要所有下拉列表都是动态的。我该怎么做?

我正在使用 Qt Creator 进行上述操作。下面是第一个组合框的 .ui 文件的代码:

widget class="QGroupBox" name="gbType">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>100</y>
      <width>281</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>12</pointsize>
     </font>
    </property>
    <property name="layoutDirection">
     <enum>Qt::LeftToRight</enum>
    </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>
    <property name="styleSheet">
     <string notr="true">border: 1px solid gray;</string>
    </property>
    <property name="title">
     <string extracomment="Type"/>
    </property>
    <widget class="QComboBox" name="cbType">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>30</y>
       <width>241</width>
       <height>27</height>
      </rect>
     </property>
     <property name="editable">
      <bool>true</bool>
     </property>
     <item>
      <property name="text">
       <string>Number</string>
      </property>
     </item>
     <item>
      <property name="text">
       <string>Alphabet</string>
      </property>
     </item>
    </widget>
   </widget>

对于第二个组合框:

<widget class="QGroupBox" name="gbCommand">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>100</y>
      <width>281</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>12</pointsize>
     </font>
    </property>
    <property name="layoutDirection">
     <enum>Qt::LeftToRight</enum>
    </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>
    <property name="styleSheet">
     <string notr="true">border: 1px solid gray;</string>
    </property>
    <property name="title">
     <string/>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
    <property name="flat">
     <bool>true</bool>
    </property>
    <property name="checkable">
     <bool>false</bool>
    </property>
    <widget class="QComboBox" name="cbCommand">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>30</y>
       <width>241</width>
       <height>27</height>
      </rect>
     </property>
     <property name="editable">
      <bool>true</bool>
     </property>
     <property name="currentText">
      <string/>
     </property>
     <item>
      <property name="text">
       <string>1</string>
      </property>
     </item>
     <item>
      <property name="text">
       <string>2</string>
      </property>
     </item>
     <item>
      <property name="text">
       <string>3</string>
      </property>
     </item>
    </widget>
   </widget>

PS:这只是一个示例设计,因此我使用数字、字母作为示例。请不要建议 UI 更改以适应要求ui,因为这超出了我的工作范围。

编辑:有多个下拉菜单需要根据之前的输入进行填充;不只是两个。

您可以将第一个组合框的 currentIndexChanged() 信号连接到检查当前选择是 "Numbers" 还是 "Alphabets" 的插槽,并根据所选内容实例化右侧的 QComboBox键入第二个并替换它。

另一种选择是将 "Command" 的所有各种组合框放在 QStackedWidget 中,然后在第一个组合框的 currentIndexChanged() 上检查当前选择是什么并将 QStackedWidget 切换到包含的页面匹配的命令组合框。