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 禁用状态的样式

相关推荐
小成202303202656 小时前
Linux高级02
linux·开发语言
知行合一。。。6 小时前
Python--04--数据容器(总结)
开发语言·python
咸鱼2.06 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H6 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐7 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子7 小时前
Java:异常(exception)
java·开发语言
lifewange7 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
cmpxr_7 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
2401_827499997 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉7 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构