Qt 之 QRadioButton

简述

QRadioButton部件提供了一个带有文本标签的单选框(单选按钮)。

QRadioButton是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮。单选框通常呈现给用户一个“多选一”的选择。也就是说,在一组单选框中,一次只能选中一个单选框。

版权声明:一去、二三里,未经博主允许不得转载。

详细描述

单选框默认开启自动互斥(autoExclusive)。如果启用了自动互斥,属于同一个父部件的单选框的行为就和属于一个互斥按钮组的一样。如果你需要为属于同一父部件的单选框设置多个互斥按钮组,把它们加入QButtonGroup中。

每当一个按钮切换选中或未选中状态时,会发出的toggled()信号。如果希望每个按钮切换状态时触发一个动作,连接到这个信号。使用isChecked()来查看特定按钮是否被选中。

就像QPushButton一样,单选框可以显示文本,以及可选的小图标。图标使用setIcon()来设置,文本可以在构造函数或通过setText()来设置。可以指定快捷键,通过在文本中的特定字符前指定一个&。

例如:

<span style="color:#000000"><code class="language-Qt">QRadioButton *button = <span style="color:#000088 !important">new</span> QRadioButton(<span style="color:#009900 !important">"Search from the &cursor"</span>, <span style="color:#000088 !important">this</span>);</code></span>

这个示例中,快捷键为Alt+c。关于更多快捷键的内容请参考:QShortcut 。如果要显示一个“&”,请使用’&&’。

示例

我们来实现一个iphone中常见的开关效果 - 单选。

效果

这里写图片描述

源码

构建单选框QRadioButton,然后将它们添加至按钮组QButtonGroup中。

<span style="color:#000000"><code class="language-Qt">QHBoxLayout <span style="color:#4f4f4f !important">*</span>pLayout <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QHBoxLayout();
m_pButtonGroup <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QButtonGroup(this);

<span style="color:#880000 !important"><em>// 设置互斥</em></span>
m_pButtonGroup<span style="color:#4f4f4f !important">-></span>setExclusive(<span style="color:#006666 !important">true</span>);
for (int i <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">0</span>; i <span style="color:#4f4f4f !important"><</span> <span style="color:#006666 !important">3</span>; <span style="color:#4f4f4f !important">++</span>i)
{
    QRadioButton <span style="color:#4f4f4f !important">*</span>pButton <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QRadioButton(this);

    <span style="color:#880000 !important"><em>// 设置文本</em></span>
    pButton<span style="color:#4f4f4f !important">-></span>setText(QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"切换%1"</span>)<span style="color:#4f4f4f !important">.</span>arg(i <span style="color:#4f4f4f !important">+</span> <span style="color:#006666 !important">1</span>));

    pLayout<span style="color:#4f4f4f !important">-></span>addWidget(pButton);
    m_pButtonGroup<span style="color:#4f4f4f !important">-></span>addButton(pButton);
}
pLayout<span style="color:#4f4f4f !important">-></span>setSpacing(<span style="color:#006666 !important">10</span>);
pLayout<span style="color:#4f4f4f !important">-></span>setContentsMargins(<span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>);

setLayout(pLayout);

<span style="color:#880000 !important"><em>// 连接信号槽</em></span>
connect(m_pButtonGroup, SIGNAL(buttonClicked(QAbstractButton<span style="color:#4f4f4f !important">*</span>)), this, SLOT(onButtonClicked(QAbstractButton<span style="color:#4f4f4f !important">*</span>)));</code></span>

槽函数,用来判断当前点击的按钮,以及获取按钮组中各个按钮的选中状态。

<span style="color:#000000"><code class="language-Qt"><span style="color:#006666 !important">void</span> MainWindow<span style="color:#006666 !important">::onButtonClicked</span>(QAbstractButton <span style="color:#4f4f4f !important">*</span>button)
{
    <span style="color:#880000 !important"><em>// 当前点击的按钮</em></span>
    qDebug() <span style="color:#4f4f4f !important"><<</span> QString(<span style="color:#009900 !important">"Clicked Button : %1"</span>)<span style="color:#4f4f4f !important">.</span>arg(button<span style="color:#4f4f4f !important">-></span>text());

    <span style="color:#880000 !important"><em>// 遍历按钮,获取选中状态</em></span>
    QList<span style="color:#4f4f4f !important"><</span>QAbstractButton<span style="color:#4f4f4f !important">*></span> <span style="color:#4f4f4f !important">list</span> <span style="color:#4f4f4f !important">=</span> m_pButtonGroup<span style="color:#4f4f4f !important">-></span>buttons();
    foreach (QAbstractButton <span style="color:#4f4f4f !important">*</span>pButton, <span style="color:#4f4f4f !important">list</span>)
    {
        QString strStatus <span style="color:#4f4f4f !important">=</span> pButton<span style="color:#4f4f4f !important">-></span>isChecked() <span style="color:#4f4f4f !important">?</span> <span style="color:#009900 !important">"Checked"</span> : <span style="color:#009900 !important">"Unchecked"</span>;
        qDebug() <span style="color:#4f4f4f !important"><<</span> QString(<span style="color:#009900 !important">"Button : %1 is %2"</span>)<span style="color:#4f4f4f !important">.</span>arg(button<span style="color:#4f4f4f !important">-></span>text())<span style="color:#4f4f4f !important">.</span>arg(strStatus);
    }
}</code></span>

样式

单选框样式

<span style="color:#000000"><code class="language-Qt"><span style="color:#000000 !important">QRadioButton</span>{
    <span style="color:#98c379">spacing</span>:<span style="color:#009900 !important"> <span style="color:#009900 !important">2</span>px</span>;
    <span style="color:#98c379">color</span>:<span style="color:#009900 !important"> white</span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span> {
    <span style="color:#98c379">width</span>:<span style="color:#009900 !important"> <span style="color:#009900 !important">45</span>px</span>;
    <span style="color:#98c379">height</span>:<span style="color:#009900 !important"> <span style="color:#009900 !important">30</span>px</span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:unchecked</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOff)</span></span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:unchecked</span><span style="color:#000000 !important">:hover</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOffHover)</span></span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:unchecked</span><span style="color:#000000 !important">:pressed</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOffPressed)</span></span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:checked</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOn)</span></span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:checked</span><span style="color:#000000 !important">:hover</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOnHover)</span></span>;
}
<span style="color:#000000 !important">QRadioButton</span><span style="color:#000000 !important">::indicator</span><span style="color:#000000 !important">:checked</span><span style="color:#000000 !important">:pressed</span> {
    <span style="color:#98c379">image</span>:<span style="color:#009900 !important"> <span style="color:#006666 !important">url(:/Images/switchOnPressed)</span></span>;
}</code></span>

上面,我们通过调用QButtonGroup的setExclusive(true)来设置按钮组中的单选框互斥。

当然,也可以设置setExclusive(false)来关闭互斥,从而实现多选功能。即使这样,如之前所言 - 强烈建议使用众所周知的约定。所以,如果要实现多选功能,建议选择QCheckBox。

更多参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值