Qt控件---按钮类型

QPushButton(普通按钮)

属性说明
text按钮中的⽂本
icon按钮中的图标
iconSize按钮中图标的尺寸
shortCut按钮对应的快捷键
autoRepeat按钮是否会重复触发. 当鼠标左键按住不放时, 如果设为 true, 则会持续产⽣鼠标点击事件; 如果设为 false, 则必须释放鼠标再次按下才能产生点击事件。
autoRepeatDelay重复触发的延时时间。按住按钮多久之后,开始重复触发
autoRepeatInterval重复触发的周期
// 获取到窗口的尺寸
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 按钮在窗口上的移动
image.png

QRadioButton(单选按钮)

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

属性说明
checkable是否能选中
checked是否已经被选中,checkable 是 checked 的前提
autoExclusive排他:选中⼀个按钮之后是否会取消其他按钮的选中。QRadioButton默认排他
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);

image.png

按钮分组

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

// 创建两个按钮组
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);

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

QCheckBox(复选按钮)

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

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);

image.png

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CHJBL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值