Qt中QRadioButton的样式设置

在 Qt 中,可以使用 Qt 样式表(QSS) 来自定义 QRadioButton 的外观。样式表类似于 CSS,允许你设置控件的颜色、字体、边框、背景等属性。

以下是如何为 QRadioButton 设置样式表的详细说明和示例。


1. 基本样式设置

你可以通过 setStyleSheet 方法为 QRadioButton 设置样式。

示例:设置文本颜色和字体
cpp 复制代码
QRadioButton *radioButton = new QRadioButton("选项 1", this);
radioButton->setStyleSheet("color: red; font-size: 16px; font-weight: bold;");
示例:设置选中和未选中状态的颜色
cpp 复制代码
radioButton->setStyleSheet(
    "QRadioButton { color: black; }"  // 默认状态
    "QRadioButton::indicator:checked { background-color: green; }"  // 选中状态
    "QRadioButton::indicator:unchecked { background-color: gray; }"  // 未选中状态
);

2. 自定义指示器(Indicator)

QRadioButton 的指示器(即单选按钮的小圆圈)可以通过样式表自定义。

示例:修改指示器的大小和形状
cpp 复制代码
radioButton->setStyleSheet(
    "QRadioButton::indicator {"
    "    width: 20px;"
    "    height: 20px;"
    "    border-radius: 10px;"  // 圆形
    "    border: 2px solid black;"
    "}"
    "QRadioButton::indicator:checked {"
    "    background-color: green;"
    "}"
    "QRadioButton::indicator:unchecked {"
    "    background-color: gray;"
    "}"
);

3. 悬停和按下状态

你可以为 QRadioButton 设置悬停(hover)和按下(pressed)状态的样式。

示例:设置悬停和按下状态
cpp 复制代码
radioButton->setStyleSheet(
    "QRadioButton {"
    "    color: black;"
    "}"
    "QRadioButton:hover {"
    "    color: blue;"  // 悬停时文本颜色
    "}"
    "QRadioButton::indicator:checked {"
    "    background-color: green;"
    "}"
    "QRadioButton::indicator:unchecked {"
    "    background-color: gray;"
    "}"
    "QRadioButton::indicator:pressed {"
    "    border: 2px solid red;"  // 按下时边框颜色
    "}"
);

4. 禁用状态

你可以为禁用的 QRadioButton 设置样式。

示例:设置禁用状态
cpp 复制代码
radioButton->setStyleSheet(
    "QRadioButton:disabled {"
    "    color: gray;"  // 禁用时文本颜色
    "}"
    "QRadioButton::indicator:disabled {"
    "    background-color: lightgray;"  // 禁用时指示器颜色
    "}"
);

5. 完整示例

以下是一个完整的示例,展示如何为 QRadioButton 设置样式表。

cpp 复制代码
#include <QApplication>
#include <QWidget>
#include <QRadioButton>
#include <QVBoxLayout>

class MyWindow : public QWidget {
public:
    MyWindow(QWidget *parent = nullptr) : QWidget(parent) {
        // 设置窗口标题
        setWindowTitle("QRadioButton 样式表示例");

        // 创建布局
        QVBoxLayout *layout = new QVBoxLayout(this);

        // 创建单选按钮
        QRadioButton *radioButton1 = new QRadioButton("选项 1", this);
        QRadioButton *radioButton2 = new QRadioButton("选项 2", this);

        // 设置样式表
        radioButton1->setStyleSheet(
            "QRadioButton {"
            "    color: black;"
            "    font-size: 14px;"
            "}"
            "QRadioButton::indicator {"
            "    width: 20px;"
            "    height: 20px;"
            "    border-radius: 10px;"
            "    border: 2px solid black;"
            "}"
            "QRadioButton::indicator:checked {"
            "    background-color: green;"
            "}"
            "QRadioButton::indicator:unchecked {"
            "    background-color: gray;"
            "}"
            "QRadioButton:hover {"
            "    color: blue;"
            "}"
            "QRadioButton::indicator:pressed {"
            "    border: 2px solid red;"
            "}"
        );

        // 将单选按钮添加到布局中
        layout->addWidget(radioButton1);
        layout->addWidget(radioButton2);
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    MyWindow window;
    window.show();

    // 运行应用程序
    return app.exec();
}

6. 样式表属性说明

以下是一些常用的样式表属性:

属性 说明
color 文本颜色
font-size 字体大小
font-weight 字体粗细(如 bold
background-color 背景颜色
border 边框(如 2px solid black
border-radius 边框圆角半径(用于圆形指示器)
width / height 指示器的宽度和高度
::indicator 指示器的样式
:checked 选中状态的样式
:unchecked 未选中状态的样式
:hover 悬停状态的样式
:pressed 按下状态的样式
:disabled 禁用状态的样式

相关推荐
Maybe_ch16 分钟前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨23 分钟前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
终焉暴龙王25 分钟前
CTFHub web进阶 php Bypass disable_function通关攻略
开发语言·安全·web安全·php
Edingbrugh.南空1 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
CodeCraft Studio2 小时前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
QQ_4376643142 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae2 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
封奚泽优2 小时前
使用Python实现单词记忆软件
开发语言·python·random·qpushbutton·qtwidgets·qtcore·qtgui
liulilittle3 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
daixin88483 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存