栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

引用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开发真的是太难找资源了,很多都只是一点点,完整的需要付费看,没充会员,所以只能自己慢慢研究。
如有侵权,私信我,马上删!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号