Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Setting "focus" true in multiple elements



  • Hi all,

    How to make both elements work with keys? Now only the first text accepts keys:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Text {
            id: thisLabel
            x: 24;
            anchors.bottom: parent.bottom
            anchors.right: parent.right
    
            property int times: 24
    
            text: "Greetings " + times
    
            font.family: "Ubuntu"
            font.pixelSize: 24
            focus: true
            color: focus?"red":"black"
            Keys.onTabPressed: ++times
        }
    
        Text {
            id: otherLabel
            x: 124;
            anchors.bottom: parent.bottom
    
            property int spacePresses: 0
    
            text: "Space pressed: " + spacePresses + " times"
    
            font.family: "Ubuntu"
            font.pixelSize: 24
    
            focus: true
    
            Keys.onEscapePressed: otherLabel.text = ' '
            Keys.onSpacePressed: increment()
    
            function increment() {
                ++spacePresses
            }
        }
    }
    

    A side question connected to this category (Mobile and Embedded). Where/How to start programming QML (QtQucik) on embedded devices, please?


  • Lifetime Qt Champion

    @qcoderpro said in Setting "focus" true in multiple elements:

    How to make both elements work with keys?

    Do you mean you want both text items to get the input at the same time? Only the one with focus gets the input. You will have to write code to also pass the input to the item without focus I think.



  • @jsulm

    Only the one with focus gets the input.

    Both have focus: true!

    You will have to write code to also pass the input to the item without focus I think.

    You mean this:

    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Text {
            id: thisLabel
            x: 24;
            anchors.bottom: parent.bottom
            anchors.right: parent.right
    
            property int times: 24
    
            text: "Greetings " + times
    
            font.family: "Ubuntu"
            font.pixelSize: 24
            color: "red"
            Keys.onTabPressed: ++times
        }
    
        Text {
            id: otherLabel
            x: 124;
            anchors.bottom: parent.bottom
    
            property int spacePresses: 0
    
            text: "Space pressed: " + spacePresses + " times"
    
            font.family: "Ubuntu"
            font.pixelSize: 24
    
            focus: true
    
            Keys.onEscapePressed: otherLabel.text = ' '
            Keys.onSpacePressed: increment()
    
            function increment() {
                ++spacePresses
                ++thisLabel.times
            }
        }
    }
    

  • Lifetime Qt Champion

    @qcoderpro said in Setting "focus" true in multiple elements:

    Both have focus: true!

    That doesn't mean that both HAVE focus. This is not how UI works. Only one UI element can have focus at a given point in time...



  • @qcoderpro
    focus handling https://doc.qt.io/qt-5/qtquick-input-focus.html

    @qcoderpro said in Setting "focus" true in multiple elements:

    A side question connected to this category (Mobile and Embedded). Where/How to start programming QML (QtQucik) on embedded devices, please?

    Nothing special to do about that, Qt is cross-platform, just learn QtQuick/QML and you will be able to write applications for embedded targets.
    here is a good ressource https://qmlbook.github.io/



  • @LeLev

    just learn QtQuick/QML and you will be able to write applications for embedded targets.

    OK. So if I want to run the simple project above on an imbedded system the steps are as follow:

    1- First I need to know the OS the embedded system is using.
    2- Compile the program for that OS and run the resulting executive file on the embedded device OS

    Right?



  • @qcoderpro that is correct



  • @LeLev

    One last question that may clarify what I'm thinking about right now.

    Apparently many of the embedded devices are running a flavor of Linux. Let's say my device is alike. Therefore we don't compile the project on that device, because it may or may not have a compiler accompanying its OS (here Linux). We, instead, compile our QML project using the compiler within the Qt Creator IDE on our desktop machine, which makes the things much easier, and afterwards copy the executable file created, specific for that OS, and just transfer it onto the device and simply run it.

    Are my assumptions right?


  • Lifetime Qt Champion

    @qcoderpro said in Setting "focus" true in multiple elements:

    compiler within the Qt Creator IDE

    QtCreator does not have any compilers. It uses compilers configured in the Kit. On Linux GCC, on Windows MSVC++ or MinGW and on MacOS clang.
    You need a cross-compiler generating binaries for your target device. For example if your device has an ARM CPU you need a compiler running on your device but generating binaries running on ARM CPU. You can't use the compiler for your host system. You also need a sysroot for your device. A sysroot contains libraries and headers from your target device and is needed to be able to compile and link your app. Often manufacturers provide a SDK containing all this: do you have a SDK?



  • @jsulm

    You need a cross-compiler generating binaries for your target device.
    For example if your device has an ARM CPU you need a compiler running on your device but generating binaries running on ARM CPU. You can't use the compiler for your host system. You also need a sysroot for your device. A sysroot contains libraries and headers from your target device and is needed to be able to compile and link your app. Often manufacturers provide a SDK containing all this: do you have a SDK?

    I don't really have a device for embedded programming. I just wanted to know the process, the steps you talked about above. But that shows that embedded programming is not merely programming. There are other stuff to know to be able to run/install the program you've already written on your computer on your device. I need to know that process.


  • Lifetime Qt Champion

    @qcoderpro said in Setting "focus" true in multiple elements:

    I need to know that process

    The process is called "cross compiling".
    You can for example tale a look at this: https://stackoverflow.com/questions/897289/what-is-cross-compilation


Log in to reply