Unable to use QML QtGamepad under Linux - qt

I am unable to use QGamepad in QML under Linux.
By "unable" I mean no gamepad signal is emitted (button pressed...)
Configuration:
Ubuntu 20.04
Qt 5.15
Logitech Gamepad F310
Gamepad is well recognized by Ubuntu:
dmesg gives:
usb 1-1.6: Product: Gamepad F310
input: Logitech Gamepad F310 as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.6/1-1.6:1.0/input/input26
jstest-gtk recognizes gamepad well:
Logitech Gamepad F310
Device: /dev/input/js0
and is responsive when pressing buttons
Sample QML code:
import QtQuick 2.15
import QtGamepad 1.15
Rectangle {
width: 320
height: 240
Gamepad {
onButtonAChanged: {
console.log("ButtonA")
}
onButtonBChanged: {
console.log("ButtonB")
}
}
}
I tried to define QT_GAMEPAD and QT_GAMEPAD_DEVICE env variables, but without any improvment.
export QT_GAMEPAD=evdev
export QT_GAMEPAD_DEVICE=/dev/input/js0
Note: the same QML code is working well under MS Windows 10 (same Qt version).

Thanks to Jack White this is the solution:
Property deviceId of Gamepad module shall be initialized, using GamepadManager.
import QtQuick 2.15
import QtGamepad 1.15
Rectangle {
width: 320
height: 240
Gamepad {
deviceId: GamepadManager.connectedGamepads.length > 0 ? GamepadManager.connectedGamepads[0] : -1
onButtonAChanged: {
console.log("ButtonA");
}
onButtonBChanged: {
console.log("ButtonB");
}
}
}

Related

How to Animate an Image with more than 256 colors (i.e. not a GIF) using AnimatedImage or otherwise

I can use AnimatedImage in Qt 5.9 which works on GIFs like this;
import QtQuick 2.7
import QtQuick.Controls 2.2
ApplicationWindow
{
visible: true
width: 640
height: 480
Rectangle
{
width: animation.width;
height: animation.height + 8
AnimatedImage
{
id: animation;
source: "myanimation.gif"
}
Rectangle {
height: 8
width: animation.currentFrame/animation.frameCount * animation.width
y: animation.height
color: "red"
Component.onCompleted: console.log("framecount ", animation.frameCount);
}
}
}
I get a lot of error messages too. This is printed over and over;
QQmlExpression: Expression qrc:/main.qml:26:20 depends on non-NOTIFYable properties:
QQuickAnimatedImage::frameCount
I took my example code from here; http://doc.qt.io/qt-5/qml-qtquick-animatedimage.html
which doen't work at all, something wrong with putting frameCount into a property, so i changed that in my version.
I would like to animate a png like an apng. Apparently there used to be mng support, but it is not there anymore.
So i call QMovie::supportedFormats which returns only GIF (on Windows).
My question:
How can i either use AnimatedImage on a format that supports non-palettised color (eg png etc) or is there another way to animate an image that would work?
Thanks for any info.
It seems like those image formats are not shipped with the standard installation of Qt/QML. You can however install them as a plugin.
See:
Documentation: Qt Image Formats
Code/GitRepository

QtQuick simple app for Raspberry

I am new to QT and want to develop a QT Quick app for the raspberry pi and its touch LCD display. The app should have several screens (sorry, not sure what the right terminology is, with screen I mean a state of the app which fills the whole display of the Raspberry Pi) and there should be buttons to switch between those screens. How is it possible to switch to a different screen when I press a button?
I tried using loader but (right now I am testing on the Desktop not the Raspberry) it opens the qml file in a new window, but I would like to have the content of the original window replaced.
Edit:
Yes, I plan using EGLFS. I enclose some code which does in principle what I want. However, I am not sure if this is the right way to do things: I put the screens I want to have into their own qml file, and toggle their visibility through buttons our mouse areas:
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 800
height: 460
title: qsTr("Hello World")
Page1 {
id: page1
visible: true
}
Page2 {
id: page2
visible: false
}
}
Page1.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
Item {
Button {
id: button1
width: 200
height: 40
text: qsTr("To second page")
onClicked: {
page2.visible=true
page1.visible=false
}
}
}
Page2.qml
import QtQuick 2.3
import QtQuick.Window 2.2
Item {
Text {
id: text1
x: 181
y: 153
text: qsTr("Second Page")
font.pixelSize: 12
}
Rectangle {
id: rectangle
x: 252
y: 222
width: 200
height: 200
color: "#000000"
border.color: "#f12525"
}
MouseArea {
id: mouseArea
x: 234
y: 209
width: 244
height: 225
onClicked:{
page1.visible=true
page2.visible=false
}
}
}
I would venture to guess that most likely you want to use Qt5's eglfs mode on the raspberry pi. This gets rid of the normal desktop and runs your Qt5 (regular or QML) app full screen. When I last did this, I needed to compile Qt5 myself. That is something you will either want to figure out cross compiling for, or use a RaspberryPi 3 (the results can then be copied to slower Raspberry Pis if desired). I worked from the guide at https://wiki.qt.io/RaspberryPi2EGLFS
It was pretty trivial to run the program then in a window on a linux desktop or full screen on a Raspberry Pi with touch screen (both the special 7" screen or a generic 22" touch display).
A newer looking alternative to using eglfs is apparently the QtWayland Compositor. I haven't used it, but there is an interesting presentation on using it for full screen embedded applications from the most recent (2017) FOSDEM, available here: https://fosdem.org/2017/schedule/event/device_specific_compositors/

How to set User Agent in QtWebEngine QML application

NOTE: I was able to do that on QtWebKit QML using the QtWebView extension. Here I am interested in using QtWebEngine.
My simple test application
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtWebEngine 1.0
ApplicationWindow {
width: 800
height: 600
color: "lightgray"
visible: true
WebEngineView {
id: webview
url: "http://stackexchange.com/"
anchors.fill: parent
}
}
How do I pass a different User Agent string?
I'm interested in using QtWebEngine too. And I can suggest you use QtWebEngine developers Trello.
As you can see in Todo for 5.5 this is currently under development and may be done in 5.5.
From Qt 5.5 onwards, you can set the WebEngineProfile.userAgent property.
// import QtWebEngine 1.4
WebEngineView {
id: webview
url: "http://stackexchange.com/"
anchors.fill: parent
profile: WebEngineProfile{
httpUserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"
}
}

Qt5/qml - Unable to loop video or audio

I'm unable to loop a media (mp4 or mp3) with Qt5.4 64 bits (gcc & clang) under Ubuntu 14.04, using QML MediaPlayer. I can't even simply replay the video or audio, it can be played once, that's all.
I add that the problem does not occur under windows 7 64 bits with Qt5.3 (mingw & msvc2012).
Here the simple test code I use :
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtMultimedia 5.0
Item {
width: 640
height: 480
MediaPlayer {
id: video
loops: 2
source : "qrc:si_blue"
}
MediaPlayer {
id: audio
loops: 2
source : "qrc:check"
}
VideoOutput {
id: output
source : video
}
Button {
id: btn
width : 30
height: 30
onClicked: audio.play()
} }

QML Video native size

I am playing a video with QML like this:
import QtQuick 2.0
import QtMultimedia 5.0
Video
{
source:'movie.mov'
width: 800
height: 600
autoPlay:true
}
which works, but I would like the movie to be displayed in its original resolution. So whatever movie file I load, I expect the Video element to scale to the video resolution. However,
import QtQuick 2.0
import QtMultimedia 5.0
Video
{
source:'movie.mov'
autoPlay:true
}
will play the video (i can hear sound) but not display anything.
Any suggestions ? (Qt 5.1)
You can bind width and height to the video resolution like this:
import QtQuick 2.0
import QtMultimedia 5.0
Video
{
source:'movie.mov'
width: metaData.resolution ? metaData.resolution.width : 0
height: metaData.resolution ? metaData.resolution.height : 0
autoPlay:true
}

Resources