Event filter in QML

1.3k views Asked by At

I have to send 48 Bytes of Data to controller via Qtcpsocket. I have represented each Bit in a Byte as a Button in QML. So whenever the user clicks the button, I have to set the Corresponding Bit to true/false and immediately send the entire 48 Bytes of data. I have so many buttons(Bits) in so many QML files. How to detect which button has been pressed and immediately set the corresponding Bit? How to get the Object in qml ? What i have done is emitting a signal in the Bit when button is pressed. Now confused how to pass it to the backend c++ on to the sockets because then i would have so many signals. I feel like it’s not a proper way to do. Any smart / better solution or similar example would be really helpful.Thanks

1

There are 1 answers

3
folibis On BEST ANSWER

I don't exactly understand from you code what are you trying to do but may be this example can help:

Byte.qml

import QtQuick 2.4
import QtQuick.Controls 1.2

Row {
    id: block
    property int number: 0
    signal dataChanged()
    Repeater {
        model: 8
        Button {
            width: 20
            height: 20
            property int buttonIndex: index
            text: buttonIndex
            checkable: true
            checked: false
            onClicked: {
                block.number = block.number ^ (1 << buttonIndex);
                dataChanged();
            }
        }
    }
}

main.qml

import QtQuick 2.4
import QtQuick.Window 2.0

Window {
    id: win
    width: 800
    height: 600
    Column {
        id: numbers
        property var bytes: []
        anchors.centerIn: parent
        Repeater {
            model: 6
            Byte {
                property int byteIndex: index
                Component.onCompleted: numbers.bytes[byteIndex] = 0;
                onDataChanged: {
                    numbers.bytes[byteIndex] = number;
                    console.log(numbers.bytes);
                }
            }
        }
    }
}

I use here 6 bytes to just bring up the idea. All you need is just to send it to C++ instead of printing it out to console. Intergrating QML and c++ is widely described in the Internet. You can start from here