-2

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

vishnu
  • 363
  • 3
  • 20
  • I have described with code here [link](https://forum.qt.io/topic/55349/event-filter-in-qml-or-how-to-detect-the-multiple-mouse-events-in-a-smart-way/3) – vishnu Jun 17 '15 at 08:24
  • 1
    Do not link to code, format your code into the question. Why can't you just use a single signal that carries the bit index and its boolean value? – cmannett85 Jun 17 '15 at 08:50

1 Answers1

1

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

folibis
  • 12,048
  • 6
  • 54
  • 97
  • I just run your program by creating a new project and adding Byte.qml file and copied your data to both Byte.qml and main.qml files.I dont know why I cannot see any output.But Indeed it gave me a hint. I am not using repeater and My buttons are in different QML files.Moreover some of the bytes in 48bytes should be of type int.Can you please come with an example of this kind. P.S: From my reputation you know that I am a beginner :P FYI – I used Qt Version 5.4.1 (MSVC 2010, 32 bit)& MinGW. Thanks. – vishnu Jun 17 '15 at 14:06
  • 1
    Sorry dude but I think my example is detailed enough to adopt it to your needs. It is not "make it for me" site. if you have troubles with QML you can start from [QML Book](http://qmlbook.github.io/) which illustrates all aspects of QML in details. – folibis Jun 17 '15 at 23:00
  • @folbis I am so sorry for asking like that.Thanks a lot for the example :) – vishnu Jun 18 '15 at 06:49