【问题标题】:Nested structures with QRadioButtons带有 QRadioButtons 的嵌套结构
【发布时间】:2017-04-20 19:17:10
【问题描述】:

我想实现类似这张图的东西:

除了顶层(“地址栏”、“表单”和“用户名...”)应该是单选按钮。

这个想法是子级别应该根据单选按钮的状态启用或禁用。并且子层应该向右移动,如图所示。

Qt 能以优雅的方式完成吗?

【问题讨论】:

    标签: qt layout qradiobutton


    【解决方案1】:

    我会说一个简单的QVBoxLayout 用于顶层,每个“子级别”都有一个QHBoxLayout,其中第一个子项是固定大小的间隔项,QVBoxLayout 包含子选项。

    然后可以通过禁用“子级别”小部件来禁用所有子选项。

    【讨论】:

      【解决方案2】:

      只需将这些子项目(如“浏览历史记录”、“收藏夹”...)放入单独的 QWidget 并将该小部件的 QWidget::setEnabled() 插槽与“地址栏”单选按钮的 QAbstractButton::toggled() 连接信号。

      这是一个 Qt Designer 的 .ui 文件(使用有效的信号槽连接,在 Designer 中尝试 Ctrl+R),它演示了想法:

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>Form</class>
       <widget class="QWidget" name="Form">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>170</width>
          <height>178</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>Form</string>
        </property>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QRadioButton" name="radioButton">
           <property name="text">
            <string>RadioButton</string>
           </property>
           <property name="checked">
            <bool>true</bool>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QWidget" name="widget" native="true">
           <layout class="QVBoxLayout" name="verticalLayout_2">
            <item>
             <widget class="QCheckBox" name="checkBox">
              <property name="text">
               <string>CheckBox</string>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QCheckBox" name="checkBox_2">
              <property name="text">
               <string>CheckBox</string>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QCheckBox" name="checkBox_3">
              <property name="text">
               <string>CheckBox</string>
              </property>
             </widget>
            </item>
           </layout>
          </widget>
         </item>
         <item>
          <widget class="QRadioButton" name="radioButton_2">
           <property name="text">
            <string>RadioButton</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QRadioButton" name="radioButton_3">
           <property name="text">
            <string>RadioButton</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
       <resources/>
       <connections>
        <connection>
         <sender>radioButton</sender>
         <signal>toggled(bool)</signal>
         <receiver>widget</receiver>
         <slot>setEnabled(bool)</slot>
         <hints>
          <hint type="sourcelabel">
           <x>84</x>
           <y>17</y>
          </hint>
          <hint type="destinationlabel">
           <x>84</x>
           <y>77</y>
          </hint>
         </hints>
        </connection>
       </connections>
      </ui>
      

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2021-02-13
        相关资源
        最近更新 更多