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);
相关推荐
hmcjn(小何同学)几秒前
轻松Linux-9.进程间通信
linux·运维·服务器·c++·bash
恋猫de小郭6 分钟前
Flutter 官方 LLM 动态 UI 库 flutter_genui 发布,让 App UI 自己生成 UI
android·前端·flutter
落羽的落羽23 分钟前
【C++】C++11的包装器:function与bind简介
c++·学习
打不了嗝 ᥬ᭄28 分钟前
【Linux】线程概念与控制
linux·c++
kymjs张涛29 分钟前
零一开源|前沿技术周刊 #15
前端·javascript·面试
reacx30 分钟前
# 第三章:状态管理架构设计 - 从 Zustand 到 React Query 的完整实践
前端
古夕31 分钟前
Vue3 + vue-query 的重复请求问题解决记录
前端·javascript·vue.js
不知名程序员第二部32 分钟前
前端-业务-架构
前端·javascript·代码规范
Bug生产工厂32 分钟前
React支付组件设计与封装:从基础组件到企业级解决方案
前端·react.js·typescript
小喷友32 分钟前
阶段三:进阶(Rust 高级特性)
前端·rust