单选按钮RadioButton

article/2025/8/25 12:29:30

效果示例:

MRadioButton.h

//********************************************************
/// @brief 自定义单选按钮
/// @author y974183789@gmail.com
/// @date 2021/9/18
/// @note 支持自定义设置单选指示器位置
/// @version 1.0.0
//********************************************************#pragma once#include <QtWidgets/QRadioButton>class MRadioButton : public QRadioButton {Q_OBJECTpublic://指示器位置,默认为左enum IndicatorPosition{kIndicatorLeft = 0,kIndicatorTop,kIndicatorRight,kIndicatorBottom,};public:explicit MRadioButton(QWidget *parent = nullptr);~MRadioButton() override;//设置指示器位置void setIndicatorPosition( IndicatorPosition pos);protected:void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;QSize sizeHint() const Q_DECL_OVERRIDE;bool hitButton(const QPoint &pos) const Q_DECL_OVERRIDE;private:IndicatorPosition m_IndicatorPosition;
};

 MRadioButton.cpp

//********************************************************
/// @brief 自定义单选按钮
/// @author y974183789@gmail.com
/// @date 2021/9/18
/// @note 支持自定义设置单选指示器位置
/// @version 1.0.0
//********************************************************#include "MRadioButton.h"
#include <QtWidgets/QStyleOptionButton>
#include <QtCore/qglobal.h>
#include <QtGui/QPainter>
#include <QtWidgets/QApplication>
#include <QtCore/QDebug>#define INDICATOR_SPACING 4MRadioButton::MRadioButton(QWidget *parent)
: QRadioButton(parent)
, m_IndicatorPosition(kIndicatorLeft)
{
}MRadioButton::~MRadioButton()
{
}void MRadioButton::setIndicatorPosition(IndicatorPosition pos)
{m_IndicatorPosition = pos;
}void MRadioButton::paintEvent(QPaintEvent *e)
{if (kIndicatorLeft == m_IndicatorPosition){return QRadioButton::paintEvent(e);}QWidget::paintEvent(e);QPainter p(this);QStyleOptionButton opt;initStyleOption(&opt);QRect rectIndicator;QRect rectLabel;auto alignment = Qt::AlignHCenter | Qt::AlignVCenter;auto pStyle = this->style();QRect indicator = opt.rect;indicator.setWidth(opt.iconSize.width());indicator.setHeight(opt.iconSize.height());int spacing = INDICATOR_SPACING;if(Q_NULLPTR != pStyle){int h = pStyle->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this);indicator.setRect(opt.rect.x(), opt.rect.y() + ((opt.rect.height() - h) / 2),pStyle->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this), h);indicator = pStyle->visualRect(opt.direction, opt.rect, indicator);spacing = pStyle->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);}switch (m_IndicatorPosition){case kIndicatorTop:{rectIndicator = QRect(QPoint(rect().center().x() - indicator.width() / 2,2), indicator.size());rectLabel = QRect(QPoint(rect().left(), rectIndicator.bottom() + INDICATOR_SPACING),rect().bottomRight());break;}case kIndicatorRight:{alignment = Qt::AlignRight | Qt::AlignVCenter;rectIndicator = QRect(QPoint(rect().right() - indicator.width(), (rect().height() - indicator.height()) / 2), indicator.size());rectLabel = QRect(rect().topLeft(), QPoint(rectIndicator.left() - spacing,rect().bottom()));break;}case kIndicatorBottom:{rectIndicator = QRect(QPoint(rect().center().x() - indicator.width() / 2, rect().bottom() - indicator.height() - INDICATOR_SPACING), indicator.size());rectLabel = QRect(rect().topLeft(), QPoint(rect().right(), rectIndicator.top() - INDICATOR_SPACING));break;}default:break;}//绘制指示器opt.rect = rectIndicator;style()->drawPrimitive(QStyle::PE_IndicatorRadioButton, &opt, &p, this);//绘制文本opt.rect = rectLabel;style()->drawItemText(&p, opt.rect, alignment | Qt::TextShowMnemonic,opt.palette, opt.state & QStyle::State_Enabled, opt.text, QPalette::WindowText);
}QSize MRadioButton::sizeHint() const
{if (kIndicatorLeft == m_IndicatorPosition|| kIndicatorRight == m_IndicatorPosition){return QRadioButton::sizeHint();}ensurePolished();QFontMetrics fm = fontMetrics();QStyleOptionButton opt;initStyleOption(&opt);QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,text()).size();sz = QSize(qMax(sz.width(), opt.iconSize.width()), sz.height() + opt.iconSize.height() + INDICATOR_SPACING * 2);return style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this);
}bool MRadioButton::hitButton(const QPoint &pos) const
{if (kIndicatorLeft == m_IndicatorPosition){return QRadioButton::hitButton(pos);}return rect().contains(pos);
}


http://chatgpt.dhexx.cn/article/0aTKPiUh.shtml

相关文章

单选按钮(RadioButton)与 复选框(CheckBox)的功能与 用法

按照惯例&#xff0c;先看看两者的区别&#xff1a; 顾名思义&#xff0c;单选俺就就是只能选中一项的&#xff0c;而复选框是可以多选的。 一般来说&#xff0c;单选按钮是用于只有唯一答案的问题的答案选项中&#xff0c;而复选框用于一些可以多选的问题的答案中。 下面看看…

Android——单选多选按钮的使用详解

一&#xff1a;单选按钮 单选按钮类&#xff1a;RadioButton android&#xff1a;checked"true"设置默认选中 单选按钮控件通常与RadioGroup搭配使用。 RadioGroup是LinearLayout的子类&#xff0c;用于将多个单选按钮组合为一组。 同一按钮组内的单选…

Android 之 RadioButton (单选按钮) Checkbox (复选框)

本节引言&#xff1a; 本节给大家带来的是Andoird基本UI控件中的RadioButton和Checkbox; 先说下本节要讲解的内容是&#xff1a;RadioButton和Checkbox的1.基本用法 2.事件处理&#xff1b; 3.自定义点击效果&#xff1b; 4.改变文字与选择框的相对位置&#xff1b; 5.修改文字…

FTP服务器-vsftpd

FTP服务基础 FTP&#xff08;File Transfer Protocol,文件传输协议&#xff09;是典型的C/S结构的应用层协议&#xff0c;需要由服务端软件、客户端软件两个部分共同实现文件传输功能。 FTP连接及传输模式 FTP服务器默认使用TCP协议的20、21端口与客户端进行通信。 20端口用…

ftpvsftpd服务

一. ftp ftp即文件传输,它是INTERNET上仍然常用的最老的网络协议之一,它为系统提供了通过网络与远程服务器传输的简单方法,FTP服务器包的名称为vsftpd 二. 部署ftp服务 1. 安装ftp yum install -y vsftpd #安装vsftpd软…

VSFTPD服务器配置

文章目录 一、实验环境二、实验目的三、实验任务任务一&#xff1a;根据以下要求配置服务器1&#xff09; 配置FTP匿名用户的主目录为/var/ftp,该账户只能下载&#xff0c;不能上传。2&#xff09;建立一个名为abc、口令xyz的FTP账户&#xff0c;设置账户具有上传、下载权限。3…

ftpd服务常见配置总结(/etc/vsftpd/vsftpd.conf)

命令端口&#xff1a; listen_port21 注意&#xff1a;该信息默认没有&#xff0c;使用的话&#xff0c;需要用户自己添加。默认是21。 主动模式端口&#xff1a; connect_from_port_20YES 主动模式端口为20&#xff08;该项配置文件默认已经添加&#xff09; ftp_data_por…

Linux系统中vsftpd配置及如何添加vsFTPd用户和设置权限

vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序。特点是小巧轻快&#xff0c;安全易用。 如果你想在你的Linux/Unix服务器上搭建一个安全、高性能、稳定性好的FTP 服务器&#xff0c;那么vsftpd可能是你的首选应用。vsftpd意思为“very secure FTP daemon(非常安全的FTP…

VSFTPD

首先&#xff0c;下载Metasploitable2-linux,在虚拟机点击文件&#xff0c;然后直接打开。 然后开启此虚拟机&#xff0c;默认用户名和密码都是msfadmin&#xff0c; 登录之后输入ifconfig查看靶机ip 打开kali&#xff0c;打开终端模拟器 输入sudo su&#xff0c;再输入用户密…

vsftp

一、概述 FTP是file Transfer Protocoll文件传输下ieyi&#xff0c;用于Internet上的文件的双向传输。因ftp是明文传输&#xff0c;没有受到保护&#xff0c;所以具有一定危险性。 VSFTP是一个基于GPL发布的类unix系统上使用的FTP服务器软件。为了解决ftp传输安全性问题的&…

vsftpd的使用

简介 vsftpd是 “very secure FTP deamon”的缩写&#xff0c;是一个完全免费&#xff0c;开源的ftp服务器软件。 特点 小巧轻快&#xff0c;安全易用&#xff0c;支持虚拟用户、支持带宽限制等功能。 安装 执行 yum -y install vsftpd 注&#xff1a;在安装前需提前确认是…

vsftpd使用指北

vsftpd使用指北 文章目录 vsftpd使用指北1.安装vsftpd2.登录3.切换本地路径下载命令&#xff1a;get用于下载单个文件&#xff1a;mget用于批量下载&#xff1a;上传命令&#xff1a; FilezillaReference vsftpd 是“very secure FTP daemon”的缩写&#xff0c;是一个完全免费…

linux安装vsftpd

linux安装vsftpd 安装vsftpd作为附件上传和下载服务 文章目录 linux安装vsftpd一、vsftpd是什么&#xff1f;二、使用步骤1.安装2.配置3.可能出现的问题第一种情况&#xff1a;selinux的ftp设置第二种情况&#xff1a;可能是防火墙的原因第三种情况&#xff1a;pam.d设置问题 …

1、vsftpd的简介

1、vsftpd的简介 vsftpd是“very secure TTP daemon”的缩写&#xff0c;是一个完全免费的、开放源代码的ftp服务器软件 2、特点 vsftpd是一款在Linux发行版中最受推崇的ftp服务器程序&#xff0c;小巧轻快&#xff0c;安全易用&#xff0c;支持虚拟用户&#xff0c;支持带宽…

vsftpd基本使用

ftp: 文件传输协议 两类连接&#xff1a; 命令连接&#xff1a;传输命令 数据连接&#xff1a;传输数据 两种模式&#xff1a; 主动模式&#xff1a;PORT 20/tcp连接客户端的命令连接使用的端口向后的第一个可用端口 被动模式&#xff1a;PASV 打开一个随机端口&#xff0c;并…

vsftpd部署流程和常见问题详解

vsftpd部署流程和常见问题详解 ⭐️ 网上关于在云服务器里配置vsftpd的文章鱼龙混杂&#xff0c;没有一篇是可以彻底解决问题的&#xff0c;有些问题虽简单&#xff0c;但也让初学者感到困惑。本文详细说明vsftpd的部署流程和一些常见问题的解决方法&#xff0c;详述用户创建过…

Linux vsFTPd服务详解——vsFTPd基础知识

今天继续给大家介绍Linux运维相关内容&#xff0c;本文主要内容是Linux的vsFTPd服务。 一、vsFTPd服务简介与安装 FTP服务&#xff0c;即File Transfer Protocol、文件传输服务&#xff0c;用于在互联网上提供文件存储和访问服务。有关FTP的原理请参见以下文章&#xff1a;FT…

vsftpd的安装和使用

目录 1、vsftpd的简介... 2 2、特点... 2 3、安装... 2 4、创建虚拟用户... 2 5、vsftpd服务器的配置... 4 6、vsftpd配置文件说明... 7 7、防火墙的配置... 8 8、vsftpd的验证... 9 9、vsftpd的常用命令... 10 10、反复需要验证ftp身份问题解决... 10 1、vsftpd的简…

Number、parseInt和parseFloat的区别

1、Number() 可以把任何类型的数据转换为数值或NaN(not a number)&#xff0c;parseInt()和parseFloat()只能把字符串或数值转换为数值&#xff1b; 2、Number()和parseFloat()会有效解析浮点数&#xff08;如"324.11abc"转为324.11&#xff09;&#xff0c;而parse…

parseFloat()函数的使用

定义&#xff1a; parseFloat() 函数可解析一个字符串&#xff0c;并返回一个浮点数。该函数指定字符串中的首个字符是否是数字。如果是&#xff0c;则对字符串进行解析&#xff0c;直到到达数字的末端为止&#xff0c;然后以数字返回该数字&#xff0c;而不是作为字符串。 语…