Qt creator and joystick. QtGamepad
-
I try use joystick Logitech Extreme 3D PRO model. In windows gaming devices window it work fine. In QTCreator it does not work. I use QtGamepad library and here
QGamepadManager* lstDevices= QGamepadManager::instance(); if (lstDevices.isEmpty()) { qDebug() << "Did not find any connected gamepads"; }
i receive "Did not find any connected gamepads" always.
In .pro file "QT += gamepad" was added
What can be wrong? -
@Sandres92 Is this really the code you have? I don't think it's going to compile.
lstDevices is a QGamepadManager.
And also lstDevices is a pointer.
Shouldn't you have something like:QGamepadManager* manager = QGamepadManager::instance(); auto lstDevices = manager->connectedGamepads(); if (lstDevices.isEmpty()) { qDebug() << "Did not find any connected gamepads"; }
-
Sorry, yes, i made mistake when write post. My real code
QGamepadManager *ptrManager = QGamepadManager::instance(); QList<int> lstDevices = ptrManager->connectedGamepads(); if (lstDevices.isEmpty()) { qDebug() << "Did not find any connected gamepads"; }
i receive "Did not find any connected gamepads
-
I've been having the same issue, did you get anywhere with it?
I think it's to do with compiling for 32 bits and 64 bits using MiniGW for Windows. When compiling 64 bits, I can't get the QGamepadManager to return any connectedgamepads. But when compiling for 32 bits, I do get one of my gamepads to show up. I have a similar problem when compiling the gamepad examples, I can only get it working on 32bit compiles.
However, in both cases, my xbox one controller is not found at all, however, on one of the examples, it does show up!
At https://doc.qt.io/qt-5/qtgamepad-index.html, there is the text:
The currently supported platforms are:
Windows (Win32 desktop, via XInput 1.3 & 1.4)
but just not sure if this is accurate.
If you did manage progress, can you share your secret?
-
I found the solution here: [#QTBUG-61553] QGamepadManager fails to detect gamepads on Windows if no QWindow was shown yet
.pro
QT += core gui gamepad
main.cpp
#include "Window.h" #include <QtGamepad/QGamepadManager> #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); do { a.processEvents(); } while (QGamepadManager::instance()->connectedGamepads().isEmpty()); Window w; w.show(); return a.exec(); }
Window.h
#ifndef WIDGET_H #define WIDGET_H #include <QtWidgets/QWidget> class Window : public QWidget { Q_OBJECT public: Window(QWidget *parent = nullptr); ~Window(); }; #endif // WIDGET_H
Window.cpp
#include "Window.h" #include <QtGamepad/QGamepadManager> #include <QtCore/QList> #include <QtCore/QDebug> Window::Window(QWidget *parent) : QWidget(parent) { QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads(); if (!gamepadIds.isEmpty()) { qDebug() << "Ok"; } else { qDebug() << "Did not find any connected gamepads"; } } Window::~Window() { }
-
You can find an introduction to usage of QtGamepad in this book: Game Programming using Qt 5 Beginner's Guide - Second Edition
My example below is based on the book example. It just for testing signals from joystick. I tested it with PS2 joystick that I bought in AliExpress here: https://aliexpress.ru/item/1005002491762700.html?spm=a2g0s.9042311.0.0.5ad733edtZrdJh&sku_id=12000020854348455 I bought three of them. And I bought two USB adapters here: https://aliexpress.ru/item/32975311818.html?spm=a2g0s.9042311.0.0.5ad733edtZrdJh&sku_id=66638635443 The next adapter works with one joystick only: https://aliexpress.ru/item/1005002517027826.html?spm=a2g0s.9042311.0.0.5ad733edtZrdJh&sku_id=12000020964236617 but the second input for joystick does not work.
Window.h
#ifndef WIDGET_H #define WIDGET_H #include <QtWidgets/QWidget> class Window : public QWidget { Q_OBJECT public: Window(QWidget *parent = nullptr); ~Window(); private slots: void axisLeftXChanged(double value); void axisLeftYChanged(double value); }; #endif // WIDGET_H
Window.cpp
#include "Window.h" #include <QtGamepad/QGamepadManager> #include <QtGamepad/QGamepad> #include <QtCore/QList> #include <QtCore/QDebug> Window::Window(QWidget *parent) : QWidget(parent) { QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads(); if (!gamepadIds.isEmpty()) { QGamepad *gamepad = new QGamepad(gamepadIds[0], this); connect(gamepad, &QGamepad::axisLeftXChanged, this, &Window::axisLeftXChanged); connect(gamepad, &QGamepad::axisLeftYChanged, this, &Window::axisLeftYChanged); } else { qDebug() << "Did not find any connected gamepads"; } } void Window::axisLeftXChanged(double value) { int direction; if (value > 0) { direction = 1; } else if (value < 0) { direction = -1; } else { direction = 0; } // m_player->setDirection(direction); // checkTimer(); qDebug() << "Direction = " << direction; } void Window::axisLeftYChanged(double value) { if (value < -0.25) { // jump(); qDebug() << "Jump"; } } Window::~Window() { }
-
I do not like QtGamepad because some times it does not work and I do not understand why. I found the best solution to me and it is using of Joystick from SDL2 library. This example works for me: SDL2/ Qt C++ not reading joystick inputs but recognized
I use PS2 joystick and USB converter. This example printed this text:
PS3/PC Gamepad 4 12 Button Clicked Button Clicked Button Clicked Button Clicked Button Clicked Button Clicked Button Clicked
*.pro
INCLUDEPATH += "E:\Libs\SDL2-2.0.12-mingw-32bit\include" LIBS += -L"E:\Libs\SDL2-2.0.12-mingw-32bit\lib" LIBS += -lSDL2
main.cpp
#include <QtWidgets/QApplication> #include "SDL2/SDL.h" #include <QDebug> #undef main int main(int argc, char *argv[]) { QApplication a(argc, argv); // MainWindow w; // w.show(); SDL_Window *window = nullptr; SDL_Surface *windowSurface = nullptr; SDL_Init(SDL_INIT_VIDEO); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1"); SDL_Init( SDL_INIT_JOYSTICK); SDL_Joystick *joystick = SDL_JoystickOpen(0); qDebug()<<SDL_JoystickName(joystick); qDebug()<<(SDL_JoystickNumAxes(joystick)); qDebug()<<(SDL_JoystickNumButtons(joystick)); window = SDL_CreateWindow("SDL CodingMadeEasy Series", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); windowSurface = SDL_GetWindowSurface(window); SDL_Event ev; int x; bool isRunning = true; while(isRunning) { while(SDL_PollEvent(&ev) != 0) { if(ev.type == SDL_JOYBUTTONDOWN) { qDebug()<<"Button Clicked"; } } // Drawing the current image to the window SDL_UpdateWindowSurface(window); } return a.exec(); }
-
Example: Joystick test application (Qt + SFML) [Windows/Linux] This example was written in Qt4 but you can run it in Qt5. You need to and "widgets" here:
QT += core gui widgets
. And replace this line#include <QtGui/QApplication>
to this#include <QtWidgets/QApplication>
in main.cpp- I built the demo: QtJoystick-SFML-exe.zip (7.39 MB)
- Source Code for Qt5: Source_QtJoystick-SFML.zip (235 KB)
I tested this example. It works with PS2 controller and USB converter: