QT控件 - 按钮(三)

QRadioButton

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

QSS:css介绍
QSS(‌Quick Style Sheet)‌是一种用于定义Qt应用程序样式的机制,‌允许开发者使用类似于CSS的语法来定义Qt应用程序的外观和风格。‌QSS的语法类似于CSS,‌通过设置属性和值的方式定义样式,‌支持不同的选择器,‌可以根据控件的类型、‌名称、‌状态等来选择应用样式。‌QSS的应用可以在资源文件中设置,‌也可以通过代码直接加载。‌此外,‌QSS还有另一种含义,‌即快速安全设置,‌它是路由器厂商推出的一种功能,‌用于快速建立与无线网卡之间的无线连接,‌简化无线安全设置的操作。‌这种QSS功能与Qt中的QSS机制不同,‌主要应用在无线网络设置中‌12。‌

//mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QRadioButton>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    /*声明两个QRadioButton对象*/
    QRadioButton *radioButton1;
    QRadioButton *radioButton2;

};
#endif // MAINWINDOW_H


//mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this ->setGeometry(0,0,800,480);
    this ->setStyleSheet("QMainwindow {background-color:rgba(200,50,100,100%);}");

    radioButton1 = new QRadioButton(this);
    radioButton2 = new QRadioButton(this);

    radioButton1 -> setGeometry(300,200,100,50);
    radioButton2 -> setGeometry(400,200,100,50);

    radioButton1 -> setText("开关1");
    radioButton2 -> setText("开关2");

    /*设置初始状态,radioButton1的checked为false,另一个为true*/
    radioButton1 -> setChecked(false);
    radioButton2 -> setChecked(true);

}

MainWindow::~MainWindow() {}

//main.cpp

#include "mainwindow.h"

#include <QApplication>
/*引入QFile*/
#include <QFile>

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

    /*指定文件*/
    QFile file(":/style.qss");

    /*判断文件是否存在*/
    if(file.exists()){
        /*以只读的方式打开*/
        file.open(QFile::ReadOnly);
        /*以字符串的方式保存读出的结果*/
        QString styleSheet = QLatin1String(file.readAll());
        /*设置全局样式*/
        qApp -> setStyleSheet(styleSheet);
        /*关闭文件*/
        file.close();
    }

    MainWindow w;
    w.show();
    return a.exec();
}

//style.css

QRadioButton{
    spacing:2px;
    color:white;
    }
QRadioButton::indicator{
    width:45px;
    height:30px;
    }
QRadioButton::indicator::unchecked{
    image: url(:/images/1.jpg);
    }
QRadioButton::indicator::checked{
    image:url(:/images/2.jpg);
    }

运行效果:
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值