当前位置: 首页 > news >正文

qt:按钮的常见操作(简单方向键项目)

1.圆形按钮

首先,设置圆形按钮,首先要将setGeometry(x位置,y位置,长,宽)中的长和宽设置为相等,再使用一下模板

   q2->setStyleSheet("QPushButton {"
                     "    background-color: black;"
                     "    color: white;"
                     "    border-radius: 25px;"
                     "    border: none;"
                     "}");

将border_radius设置为长或宽的一半,就可以得到一个圆形按钮了

 

 

2.图片按钮

   cq->setIcon(icon);
   cq->setIconSize(QSize(100,50));

那么问题来了,如何在图片上显示字呢?

最好的方法是在图片上直接p,如果使用样式表就需要绝对路径,一般不会使用绝对路径 

3.按钮快捷键

   方法

up->setShortcut(QKeySequence("w"));

up是按钮,而且w的位置也可以是ctrl+w等组合键

在使用这钟快捷键方式后,不点击小兔子,点键盘上的wasd,就能控制猫的移动了

4. 重复触发

键盘:默认支持

鼠标:setAutoReapeat,setAutoReapeatDelay(触发延迟),setAutoReapeatInterval(触发间隔)

  up->setAutoRepeat(true);
    down->setAutoRepeat(true);
     left->setAutoRepeat(true);
      right->setAutoRepeat(true);

现在按住兔子,猫就可以一直向上了

5.选择按钮

QRaioButton,属性:checkable:能否被选中,true表示可以被选中,false表示不可以,checked,表示是否已经被选中

对应槽函数,clicked是点击时,pressed是鼠标按下时,released是鼠标释放时,toggled是checked属性改时(最适合)

所有按钮默认排他,QButtonGroup可以进行分组,组内排他,QCheckbox可以复选

应用:模拟菜单(选择标题可以将按钮和标签放入一个显示组里面管理,具体可以看代码)

注意,QButtonGroup默认排他,在对复选按钮进行管理时需要setExclusive函数取消排他性,要设置标签的话可以在旁边搞一个不可选中的黑底白字按钮,或进行显示控制(之后会讲)

代码

pro

QT       += core gui
QMAKE_PROJECT_DEPTH = 0

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 .h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPushButton>
#include<QRadioButton>
#include<QCheckBox>
#include<QButtonGroup>
#include<QGroupBox>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();
public slots:
    void getup();
    void getdown();
    void getleft();
    void getright();
private:
    Ui::MainWindow *ui;
    QPushButton*example;
    QPushButton*up;
    QPushButton*down;
    QPushButton*left;
    QPushButton*right;
    QRadioButton*q1;
    QRadioButton*q2;
    QCheckBox*q3;
    QCheckBox*q4;
    QCheckBox*q5;
   QButtonGroup*g1;
    QButtonGroup*g2;
};
#endif // MAINWINDOW_H

main

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QLabel>
#include<QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    example=new QPushButton(this);

    up=new QPushButton(this);
    down=new QPushButton(this);
    left=new QPushButton(this);
    right=new QPushButton(this);
    example->setGeometry(100,100,50,50);
     up->setGeometry(375,200,100,100);
     down->setGeometry(375,400,100,100);
     left->setGeometry(275,300,100,100);
     right->setGeometry(475,300,100,100);
    QIcon cat("D:/project/cat.jpg");
    example->setIcon(cat);
    example->setIconSize(QSize(55,75));
     QIcon upsign("D:/project/p1.jpg");
    up->setIcon(upsign);
     up->setIconSize(QSize(100,100));
    down->setIcon(upsign);
    down->setIconSize(QSize(100,100));
    left->setIcon(upsign);
    left->setIconSize(QSize(100,100));
    right->setIcon(upsign);
    right->setIconSize(QSize(100,100));
    up->setShortcut(QKeySequence("w"));
     down->setShortcut(QKeySequence("s"));
     left->setShortcut(QKeySequence("a"));
      right->setShortcut(QKeySequence("d"));
     up->setAutoRepeat(true);
    down->setAutoRepeat(true);
     left->setAutoRepeat(true);
      right->setAutoRepeat(true);
    connect(up,&QPushButton::clicked,this,&MainWindow::getup);
    connect(down,&QPushButton::clicked,this,&MainWindow::getdown);
    connect(left,&QPushButton::clicked,this,&MainWindow::getleft);
    connect(right,&QPushButton::clicked,this,&MainWindow::getright);
    q1=new QRadioButton("麦辣鸡腿",this);
    q1->setGeometry(600,100,100,100);
    q2=new QRadioButton("双层吉士",this);
    q2->setGeometry(500,100,100,100);
    q3=new QCheckBox("蛋挞",this);
    q3->setGeometry(100,200,100,100);
    q4=new QCheckBox("可乐",this);
    q4->setGeometry(100,300,100,100);
    q5=new QCheckBox("雪碧",this);
    q5->setGeometry(100,400,100,100);
    g1=new QButtonGroup(this);
    g2=new QButtonGroup(this);
    g1->addButton(q1);
    g1->addButton(q2);
    g2->setExclusive(false);
    g2->addButton(q3);
    g2->addButton(q4);
   g2->addButton(q5);
    //QLabel*l=new QLabel("汉堡",this);
  //  l->setGeometry(400,100,100,100);
  //  QVBoxLayout *layout = new QVBoxLayout(this);
   // layout->addWidget(l);
   // layout->addWidget(q1);
   // layout->addWidget(q2);


}
void MainWindow::getup()
{
    QPoint mypos = example->pos();
  mypos.setY(mypos.y() -5);
    example->move(mypos);
}void MainWindow::getdown()
{
    QPoint mypos = example->pos();
    mypos.setY(mypos.y() +5);
    example->move(mypos);
}
void MainWindow::getleft()
{
    QPoint mypos = example->pos();
    mypos.setX(mypos.x() - 5);
    example->move(mypos);
}
void MainWindow::getright()
{
    QPoint mypos = example->pos();
    mypos.setX(mypos.x() + 5);
    example->move(mypos);
}
MainWindow::~MainWindow()
{
    delete ui;
}

相关文章:

  • 【Web前端开发精品课 HTML CSS JavaScript基础教程】第二十四章课后题答案
  • Python操作MySQL
  • 索引以及索引底层数据结构
  • 【Elasticsearch】Retrieve inner hits获取嵌套查询的具体的嵌套文档来源,以及父子文档的来源
  • leetcode203.移除链表元素
  • 幂等与分布式锁的区别及应用场景
  • 前端自动化部署的极简方案
  • windows下docker使用笔记
  • 项目中一些不理解的问题
  • 解决Python升级导致PySpark任务异常方案
  • idea 无法下载源码
  • R-CNN
  • Java 中有哪些常见的语法糖?
  • 如何安装Hadoop
  • 旧手机热点无法提供ipv6解决方法(emui 8 热点提供ipv6)
  • 跳表(Skip List)详解
  • 【Cesium学习(十二)】Cesium常见问题整理总结
  • CSS基本选择器
  • 关于 形状信息提取的说明
  • Redis 的常见应用场景
  • 850亿元!2025年中央金融机构注资特别国债(一期)拟第一次续发行
  • 新买宝马竟是“维修车”,男子发视频维权被4S店索赔100万
  • 遇冰雹天气,西安机场新航站楼成“水帘洞”
  • 追光|铁皮房、土操场,这有一座“筑梦”摔跤馆
  • 咖啡戏剧节举办第五年,上生新所“无店不咖啡,空间皆可戏”
  • 60岁济南石化设计院党总支书记、应急管理专家李有臣病逝