Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT qml Button emits Signal clicked although disabled

Tags:

c++

qt

qml

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;
}
like image 330
Christian D Avatar asked Oct 30 '17 12:10

Christian D


People also ask

What is onclicked signal in Qt?

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.

Who can see the signal sent by the QML code?

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.

What is a QML property change signal?

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.

What is signal handler in QML?

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.


1 Answers

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.

like image 125
Manthan Tilva Avatar answered Sep 29 '22 10:09

Manthan Tilva