Christian D
Christian D

Reputation: 61

QT qml Button emits Signal clicked although disabled

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;
}

Upvotes: 6

Views: 7482

Answers (2)

Th. Thielemann
Th. Thielemann

Reputation: 2824

Disable the button on clicked. But enable the button from outside.

Button {
  id: myButton
  onClicked: {
      enable = false
      worker.doAction()
  }
}

Worker {
  id: worker
  onDoAction: {
      // do something
      button.enable = true
  }
}

BTW: It is not necessary to surround the id of an item with qoutes.

Upvotes: 2

Manthan Tilva
Manthan Tilva

Reputation: 3277

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.

Upvotes: 2

Related Questions