2016-06-15 10 views
0

Ich habe eine Schnittstelle erstellt hat eine ListView und zwei Buttons. Wenn Sie auf Scan Schaltfläche klicken, wird C++ aufgerufen und Änderungen am Modell von ListView vorgenommen. Danach wird C++ ein Signal aussenden, um das Modell zu ändern, daher wird ListView in QML mit dem neuen Modell aktualisiert. Ich möchte, dass BusyIndicator während dieses Vorgangs ausgeführt wird. Wie kann ich das machen ?.Machen Sie BusyIndicator laufen, wenn Sie auf die Schaltfläche klicken (Signal an C++)

Ich sah ein paar Lösungen auf stackoverflow wie diese: BusyIndicator does not show up aber keiner von ihnen arbeitete für meinen Fall.

Kann mir jemand helfen? Vielen Dank.

Hier ist mein qml Code:

import QtQuick 2.5 
import QtQuick.Layouts 1.3 
import Qt.labs.controls 1.0 

Rectangle 
{ 
    objectName: "bluetoothPage" 
    anchors.fill: parent 

    property var bluetoothDataModel: messageFromApp.bluetoothData 
    onBluetoothDataModelChanged: listView.update() 

    signal qmlScanButtonSignal() 
    signal qmlDisconnectButtonSignal() 

    ColumnLayout 
    { 
     anchors.fill: parent 
     spacing: 6 

     RowLayout 
     { 
      Layout.fillWidth: true 

      Text 
      { 
       Layout.fillWidth: true 
       text: "Connect with ECU" 
       font.bold: true 
       font.pixelSize: 20 
      } 

      BusyIndicator 
      { 
       id: busyIndicator 
       Layout.preferredWidth: 30 
       Layout.preferredHeight: 30 
       running: false 
       visible: false 
      } 
     } 

     GroupBox 
     { 
      Layout.fillHeight: true 
      Layout.fillWidth: true 
      title: qsTr("Available device:") 

      ListView 
      { 
       id: listView 
       anchors.fill: parent 
       model: bluetoothDataModel 
       delegate: Component 
          { 
           Item 
           { 
            width: parent.width 
            height: 40 
            Column 
            { 
             Text { text: "Name:" + model.modelData.name } 
             Text { text: "Number:" + model.modelData.macAddress } 
            } 
            MouseArea 
            { 
             anchors.fill: parent 
             onClicked: listView.currentIndex = index 
            } 
           } 
          } 
       highlight: Rectangle 
          { 
           color: "blue" 
          } 
      } 
     } 

     RowLayout 
     { 
      Layout.fillWidth: true 
      Layout.preferredHeight: 10 

      Button 
      { 
       Layout.fillHeight: true 
       Layout.fillWidth: true 
       text: "Scan" 
       onClicked: qmlScanButtonSignal() 
      } 

      Button 
      { 
       Layout.fillHeight: true 
       Layout.fillWidth: true 
       text: "Disconnect" 
       onClicked: qmlDisconnectButtonSignal() 
      } 
     } 
    } 

} 

Antwort

0

Nur diese Lösung für mich in meinem Fall gearbeitet. Allerdings, wie jeder gesagt hat mit QCoreApplication::processEvents() ist wirklich eine schlechte Praxis. Ich versuche auch, QThread zu verwenden, aber es stürzte ab, wenn das Signal innerhalb des Threads ausgegeben wurde. Wenn Sie irgendwelche weiteren Lösungen haben, lassen Sie mich jetzt. Ich bin wirklich dankbar. Vielen Dank.

QML

BusyIndicator { 
    running: CPPModule.busy 
} 

CPP

void CPPModule::setBusy(const bool &busy) 
{ 
    m_busy = busy; 
    emit busyChanged(); 
} 
void CPPModule::InsertIntoDB() 
{ 
    setBusy(true); 
    QThread::msleep(50); 
    QCoreApplication::processEvents(); 
    /* 
    very Long Operation 
    */ 
    setBusy(false); 
} 

Eine andere Lösung ist dies:

Timer { 
    id: myTimer 
    interval: 1 
    onTriggered: { 
     app.someLongRunningFunction(); 
     myActivityIndicator.visible = false; 
    } 
} 

Butoon{ 
    .... 
    onClicked: { 
     myActivityIndicator.visible=true; 
     myTimer.start(); 
    } 
}