与QRadioButtons
问题描述:
嵌套结构我想实现类似于此图的内容:与QRadioButtons
除了顶层(“您的地址栏”,“形式”和“用户名......”)应该是单选按钮。
这个想法是,应根据单选按钮的状态启用或禁用子级。应该像图片上的那样向右移动子平面。
这可以通过优雅的方式完成Qt吗?
答
我会说一个简单的QVBoxLayout
顶级和每个“子级”有一个QHBoxLayout
与固定大小的间隔项目作为第一个孩子和QVBoxLayout
包含子选项。
禁用所有子选项可以简单地通过禁用“sublevel”小部件来完成。
答
只需将这些子项目(如“浏览历史记录”,“收藏夹”...)分隔成QWidget
,并使用“地址栏”单选按钮的QAbstractButton::toggled()
信号将该小部件的QWidget::setEnabled()
插槽连接起来。
这是一个Qt设计师的的.ui文件(工作信号槽连接,请尝试按Ctrl +[R在Designer)演示的想法:
<?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>