2016-06-15 4 views
0

私はListViewと2つのボタンを持つインターフェースを作成しました。 ボタンをクリックすると、C++を呼び出してListViewのモデルに変更します。その後、C++はモデルの変更通知を出すので、QMLのListViewは新しいモデルで更新されます。そのプロセス中にBusyIndi​​catorを実行させたい。どうやってやるの ?。ボタンをクリックするとBusyIndi​​catorが実行されます(C++への信号)

私はこのようなstackoverflowのいくつかのソリューションを見た:BusyIndicator does not show upしかし、私の場合はそれらのどれも働いていませんでした。

誰でも手伝ってもらえますか?ありがとう。ここで

は私のQMLコードです:

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() 
      } 
     } 
    } 

} 

答えて

0

のみ、このソリューションは、私の場合は私のために働きました。しかし、誰もが言ったようにQCoreApplication::processEvents() は本当に悪い習慣です。私もQThreadを使用しようとしましたが、スレッド内でシグナルが放射されるとクラッシュしました。もしあなたの男が何か解決策を持っていれば、今私に教えてください。私は本当に感謝しています。ありがとう。

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

別の解決策はこれです:

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

Butoon{ 
    .... 
    onClicked: { 
     myActivityIndicator.visible=true; 
     myTimer.start(); 
    } 
} 
関連する問題