I am working on a QT Quick Controls 2 application for Android and using Qt Bluetooth for communication with the device.
By clicking a button (using the onClicked()
signal) the app calls a function of my c++ class for the Bluetooth communication. It sends the command to the device and waits for an answer. So far it´s working well.
Now my Problem:
I disable the button when clicked, call the Bluetooth function, then enable the button again, to prevent multiple clicks while waiting for an answer, but however, the button emits the onClicked()
signal while waiting although its property enabled is false.
When I don't enable the button once the Bluetooth dialog is finished, it can only be clicked once (like expected), but I want it to be enabled again.
Multiple emission of the signal causes relevant problems on the hardware backend.
Any idea how to fix this?
Button onClicked() signal:
bEdit.onClicked: {
bEdit.enabled = false;
btConnect.fill("1", "30");
bEdit.enabled = true;
}
Bluetooth write and read:
unsigned int Bluetooth::fill(QString slot, QString volume)
{
QString output = ("CK Fill " + slot + " " + volume + "\r\n");
QByteArray baOutput = output.toLatin1();
static const QString serviceUuid(QStringLiteral("00001101-0000-1000-8000-00805F9B34FB"));
socket->connectToService(QBluetoothAddress("98:d3:32:20:46:b9"), QBluetoothUuid(serviceUuid), QIODevice::ReadWrite);
socket->write(baOutput);
QString input = "";
while(input == "")
{
input = socket->readAll();
}
qDebug() << input;
return 0;
}
For example, the Button type from the Qt Quick Controls module has a clicked signal, which is emitted whenever the button is clicked. In this case, the signal handler for receiving this signal should be onClicked.
Only users with topic management privileges can see it. Right now I have Qml code sending a signal to my c++ code that sends a true or false. When the button is clicked it sends a signal containing true.
A signal is automatically emitted when the value of a QML property changes. This type of signal is a property change signal and signal handlers for these signals are written in the form on<Property>Changed, where <Property> is the name of the property, with the first letter capitalized. For example, the MouseArea type has a pressed property.
QML has a signal and handler mechanism, where the signal is the event and the signal is responded to through a signal handler. When a signal is emitted, the corresponding signal handler is invoked.
I have not work on bluetooth, but I had come across same situation while working on REST api's with QT QML. Your button click signal is not disable because qt event loop is not get called after changing visibility property of button. You can try below work around using signal/slot.
ApplicationWindow {
id:"root"
signal activated()
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button{
id: "button"
text : "Button"
visible : true
enabled : true
onClicked: {
button.enabled=false;
root.activated()
}
}
onActivated:{
btConnect.fill("1", "30");
button.enabled=true;
}
}
Here we disable button then emit signal. In slot of this signal you can do your backend work, once you done with work enable button again. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With