Qt控件---按钮类型

文章目录

QPushButton(普通按钮)

属性 说明
text 按钮中的⽂本
icon 按钮中的图标
iconSize 按钮中图标的尺寸
shortCut 按钮对应的快捷键
autoRepeat 按钮是否会重复触发. 当鼠标左键按住不放时, 如果设为 true, 则会持续产⽣鼠标点击事件; 如果设为 false, 则必须释放鼠标再次按下才能产生点击事件。
autoRepeatDelay 重复触发的延时时间。按住按钮多久之后,开始重复触发
autoRepeatInterval 重复触发的周期
cpp 复制代码
// 获取到窗口的尺寸
int w = this->geometry().width();
int h = this->geometry().height();

QPushButton *user = new QPushButton(this);
QPushButton *s = new QPushButton(this);
QPushButton *x = new QPushButton(this);
QPushButton *z = new QPushButton(this);
QPushButton *y = new QPushButton(this);

// 设置按钮位置和尺寸
user->setGeometry(0, 0, 60, 60);
s->setGeometry(310, 390, 61, 31);
x->setGeometry(310, 480, 61, 31);
z->setGeometry(240, 430, 61, 31);
y->setGeometry(380, 430, 61, 31);

// 设置按钮图标
user->setIcon(QIcon(":/img/1.jpeg"));
user->setIconSize(QSize(50, 50));
s->setIcon(QIcon(":/img/shang.png"));
x->setIcon(QIcon(":/img/xia.png"));
z->setIcon(QIcon(":/img/zuo.png"));
y->setIcon(QIcon(":/img/you.png"));

// 开启按钮重复触发
s->setAutoRepeat(true);
x->setAutoRepeat(true);
z->setAutoRepeat(true);
y->setAutoRepeat(true);

// 设置按钮键盘快捷键
s->setShortcut(QKeySequence("w"));
x->setShortcut(QKeySequence("s"));
z->setShortcut(QKeySequence("a"));
y->setShortcut(QKeySequence("d"));

// 设置按钮槽函数
connect(s, &QPushButton::clicked, this, [=](){
    QRect g = user->geometry();
    // 设置移动的位置,注意边界
    if(g.y() <= 10)
        user->setGeometry(g.x(), 0, g.width(), g.height());
    else
        user->setGeometry(g.x(), g.y() - 10, g.width(), g.height());
});
connect(x, &QPushButton::clicked, this, [=](){
   QRect g = user->geometry();
   // 设置移动的位置,注意边界
   if(g.y() >= h - 70)
       user->setGeometry(g.x(), h - 60, g.width(), g.height());
   else
       user->setGeometry(g.x(), g.y() + 10, g.width(), g.height());
});
connect(z, &QPushButton::clicked, this, [=](){
    QRect g = user->geometry();
    // 设置移动的位置,注意边界
    if(g.x() <= 10)
        user->setGeometry(0, g.y(), g.width(), g.height());
    else
        user->setGeometry(g.x() - 10, g.y(), g.width(), g.height());
});
connect(y, &QPushButton::clicked, this, [=](){
    QRect g = user->geometry();
    // 设置移动的位置,注意边界
    if(g.x() >= w - 70)
        user->setGeometry(w - 60, g.y(), g.width(), g.height());
    else
        user->setGeometry(g.x() + 10, g.y(), g.width(), g.height());
});

通过 s x z y 这四个按钮来控制 user 按钮在窗口上的移动

QRadioButton(单选按钮)

QRadioButton 是单选按钮,可以在多个选项中选择一个

属性 说明
checkable 是否能选中
checked 是否已经被选中,checkable 是 checked 的前提
autoExclusive 排他:选中⼀个按钮之后是否会取消其他按钮的选中。QRadioButton默认排他
cpp 复制代码
QLabel *l = new QLabel(this);
l->setText("你的性别是:");

// 创建单选按钮并设置文本
QRadioButton *man = new QRadioButton(this); man->move(0, 50);
QRadioButton *woman = new QRadioButton(this); woman->move(0, 100);
QRadioButton *later = new QRadioButton(this); later->move(0, 150);
man->setText("男");
woman->setText("女");
later->setText("稍后选择");

// 设置稍后选择为默认
later->setChecked(true);

按钮分组

因为单选按钮默认是排他的,所以当有多个问题的选择时,如果不讲按钮分开那整个程序就只能选择一个单选按钮。因此需要使用 QButtonGroup 对所有的按钮进行分组操作。每个单选按钮的排他属性只会对自身组内的单选按钮生效。

cpp 复制代码
// 创建两个按钮组
QButtonGroup *g1 = new QButtonGroup(this);
QButtonGroup *g2 = new QButtonGroup(this);

// 创建第一组单选按钮并设置文本
QLabel *l1 = new QLabel(this);
l1->setText("你的性别是:");

QRadioButton *man = new QRadioButton(this); man->move(0, 50);
QRadioButton *woman = new QRadioButton(this); woman->move(0, 100);
man->setText("男");
woman->setText("女");

// 创建第二组单选按钮并设置文本
QLabel *l2 = new QLabel(this);
l2->setText("你的学历是:");
l2->move(0, 150);

QRadioButton *ben = new QRadioButton(this); ben->move(0, 200);
QRadioButton *zhuan = new QRadioButton(this); zhuan->move(0, 250);
ben->setText("本科");
zhuan->setText("大专");

// 将单选按钮进行分组
g1->addButton(man); g1->addButton(woman);
g2->addButton(ben); g2->addButton(zhuan);

分组之后不同组的按钮就不会互相影响了

QCheckBox(复选按钮)

QCheckBox 表示复选按钮,可以允许选中多个。

cpp 复制代码
QLabel *l = new QLabel(this);
l->setText("你喜欢什么运动:");
QCheckBox *ball = new QCheckBox(this);
ball->setText("打球"); ball->move(0, 50);
QCheckBox *run = new QCheckBox(this);
run->setText("跑步"); run->move(0, 100);
QCheckBox *swim = new QCheckBox(this);
swim->setText("游泳"); swim->move(0, 150);
相关推荐
小短腿的代码世界37 分钟前
Qt 股票订单撮合引擎:高频交易系统的核心心脏
开发语言·数据库·qt·系统架构·交互
不会敲代码142 分钟前
手写 Zustand:三十分钟带你搞懂状态管理库的核心原理
前端·javascript·源码
神奇的程序员1 小时前
重构了自己5年前写的截图插件
前端·javascript·架构
REDcker1 小时前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
UXbot2 小时前
一人独立交付 UI + 前端:AI 驱动 UI 设计工具的五大功能模块深度评测
前端·低代码·ui·设计模式·交互
kobesdu2 小时前
【ROS2实战笔记-19】ROS2 生命周期节点的启动顺序、状态转换陷阱与热备方案
java·前端·笔记·机器人·ros·ros2
诚实可靠王大锤2 小时前
React Native 输入框与按钮焦点冲突解决方案(rn版本0.70.3)
前端·javascript·react native·react.js
kyriewen3 小时前
测试妹子让我写单测,我偷偷用AI一天干完一周的活
前端·chatgpt·cursor
2601_957780843 小时前
Claude Code 2026年最新部署指南:从环境搭建到技能扩展
前端·人工智能·ai编程·claude
王老师青少年编程3 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:合并序列
c++·字符串·csp·高频考点·信奥赛·字符串排序·合并序列