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);
相关推荐
黑不溜秋的19 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光24 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
海绵波波10732 分钟前
Qt操作主/从视图及XML——实例:汽车管理系统
xml·qt·汽车
￴ㅤ￴￴ㅤ9527超级帅38 分钟前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
Fairy_sevenseven1 小时前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
zqx_71 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
Death2001 小时前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
六点半8881 小时前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法
惜.己1 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5