引用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