QT与游戏手柄测试(数据与UI相连,ui界面作出反应)

本文详细介绍了使用QT进行游戏手柄的UI界面展示和事件响应的实现过程,包括手柄轴位移和按键状态的捕获,并通过修改UI样式实时反馈按键状态。代码示例展示了如何连接和处理各种游戏手柄输入,为QT新手提供了宝贵的参考资料。
摘要由CSDN通过智能技术生成

引用QT野路子游戏手柄测试那章的代码进行进一步修改
链接如下:

https://blog.csdn.net/laorenshen/article/details/123421952

一、先进行的是UI界面展示

在这里插入图片描述

二、只要对mainwindow.cpp文件进行修改

mainwindow.cpp展示:

#include "mainwindow.h"
#include "ui_mainwindow.h"

const QString BTN_color("background-color: red;");
const QString BTN_color2("background-color: green;");

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //连接设备
    QGamepad *m_gamepad = new QGamepad(0, this);

    connect(m_gamepad, &QGamepad::axisLeftXChanged, this, [](double value){
        qDebug() << "Left X" << value;
    });
    connect(m_gamepad, &QGamepad::axisLeftYChanged, this, [](double value){
        qDebug() << "Left Y" << value;
    });
    connect(m_gamepad, &QGamepad::axisRightXChanged, this, [](double value){
        qDebug() << "Right X" << value;
    });
    connect(m_gamepad, &QGamepad::axisRightYChanged, this, [](double value){
        qDebug() << "Right Y" << value;
    });
    connect(m_gamepad, &QGamepad::buttonAChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_A->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_A->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button A" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonBChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_B->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_B->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button B" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonXChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_X->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_X->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button X" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonYChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_Y->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_Y->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Y" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonL1Changed, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_L1->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_L1->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button L1" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonR1Changed, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_r1->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_r1->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button R1" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonL2Changed, this, [](double value){
        qDebug() << "Button L2: " << value;
    });
    connect(m_gamepad, &QGamepad::buttonR2Changed, this, [](double value){
        qDebug() << "Button R2: " << value;
    });
    connect(m_gamepad, &QGamepad::buttonSelectChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_SELECT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_SELECT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Select" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonStartChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_START->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_START->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Start" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonGuideChanged, this, [](bool pressed){
        qDebug() << "Button Guide" << pressed;
    });

    connect(m_gamepad, &QGamepad::buttonUpChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_UP->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_UP->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonUp" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonDownChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_DOWN->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_DOWN->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonDown" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonLeftChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_LEFT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_LEFT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonLeft" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonRightChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_RIGHT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_RIGHT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonRight" << pressed;
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

三、现在正在进行后续开发

为什么要把这些东西写出来,是因为我作为一个新手进行QT开发真的是太难找资源了,很多都只是一点点,完整的需要付费看,没充会员,所以只能自己慢慢研究。
如有侵权,私信我,马上删!

完善版下一章:

https://blog.csdn.net/Nuky_/article/details/125595096?spm=1001.2014.3001.5501

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nutkey

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

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

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

打赏作者

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

抵扣说明:

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

余额充值