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

相关推荐

  1. Qt 在编辑框中加入按钮

    2024-04-13 04:50:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 04:50:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 04:50:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 04:50:03       82 阅读
  4. Python语言-面向对象

    2024-04-13 04:50:03       91 阅读

热门阅读

  1. 一个神奇的 Python 库——Ray

    2024-04-13 04:50:03       44 阅读
  2. HTML5新增元素

    2024-04-13 04:50:03       76 阅读
  3. lisp学习历程

    2024-04-13 04:50:03       39 阅读
  4. 我的lisp学习历程

    2024-04-13 04:50:03       35 阅读
  5. 蓝桥集训之三国游戏

    2024-04-13 04:50:03       83 阅读
  6. Mybatis学习&面试题

    2024-04-13 04:50:03       43 阅读
  7. Frida 远程RPC 调用进阶

    2024-04-13 04:50:03       34 阅读