2016-04-14 19 views
0

2つのqmlファイルがあります。 Toolbarbutton.qmlはボタンを作成し、DockWidget.qmlはListViewを作成します。ボタンがクリックされたことをDockWidgetにToolbarbuttonブロードキャストで通知するようにしています。私は次に私のlistViewに項目を追加したい。信号を使用して通信する2つのQMLファイルの取得に問題があります

私は、通信を確立するために信号を使用しようとしています。 Toolbarbutton.qmlには、IDがsaveButtonのRectangleアイテムがあります。このRectangle Itemの下でaddSaveHistory()というシグナルを追加しました。コードを短くして、自分が行っていることを見やすくしました。

Rectangle { 
id: saveButton 
width: 50 
height: 30 
border.color: "white" 
color: buttonMouseArea.containsMouse ? "grey" : "black" 

//Button text 
Text{ 
    id: buttonLabel 
    anchors.centerIn: parent 
    text: "Save +" 
    color: "white" 
} 
signal addSaveHistory(string url) 

MouseArea{ 
    id: buttonMouseArea 
    anchors.fill: parent //anchor the mousearea to the rect area 

    //onClicked handles valid mouse button clicks 
    onClicked: { 
     addSaveHistory("Hello?") //dispatch the event 

    } 
} 
} 

DockWidget.qmlファイルには、Connectionsコンポーネントを使用しているアイテムがあります。私はコードを短くして、わたしがやっていることを見やすくします。

Item{ 
    width: 100 
    height: 100 
    objectName: "Save + History" 

Rectangle { 
    id: rect 
    anchors.fill: parent 
    color: "#323232" 

Connections { 
    target: saveButton //id of rectangle in ToolbarButton.qml 

    onAddSaveHistory: { 

     // this is never called 
    } 
} 
} 

なぜ、onAddSaveHistoryが呼び出されないのかわかりません。また、Dockwidget.qmlファイルをToolbarbutton.qmlにロードしてからDockwidgetで関数を呼び出すためにLoaderコンポーネントを使用しようとしましたが、うまくいきません。

信号がどのように機能するかについて間違った考えがありますか?どんな指導にも大いに感謝します。

ここに私のmain.qmlファイルがあります。

import QtQuick 2.2 
import Painter 1.0 

import "save.js" as Save 

Plugin { 

//executed at startup 
Component.onCompleted:{ 

    //add toolbar 
    alg.ui.addToolBarWidget("ToolbarButton.qml")//add a button to toolbar 
    alg.ui.addDockWidget("DockWidget.qml")//add dock widget 

    Save.log("Incremental Save Plugin has been created") 

    } 
} 

答えて

1

まず第一に、信号がToolbarButton.qmlファイル、すなわち長方形のルートにQML要素で宣言されていることを実感。ルート項目のみがQML要素のインタフェースを形成します。すべての子要素は外界に隠されています。だから、ファイルの先頭近くにシグナルを宣言するのが通例です。一般的な順序は次のとおりです。

Item { 
    id: root 
    property real foo: 0.0 
    property alias bar: innerItem.bar 
    signal baz(url location) 

    Rectangle { 
     id: innerItem 
     property color bar: "red" 
    } 
} 

したがって、この場合の信号を宣言し、あなたが含まれているMouseAreaでクリックしたときにそれを放出する簡単なToolbarButton.qmlは次のようになります。そして、

import QtQuick 2.3 

Rectangle { 
    id: root 
    width: buttonLabel.width + 20 
    height: buttonLabel.height + 20 
    color: "steelBlue" 

    // Declare signal on the button object 
    signal addSaveHistory(string url) 

    Text { 
     id: buttonLabel 
     anchors.centerIn: parent 
     text: "Save +" 
    } 

    MouseArea { 
     id: buttonMouseArea 
     anchors.fill: parent 
     onClicked: { 
      // emit the signal 
      root.addSaveHistory("Hello?") 
     } 
    } 
} 

シンプル

import QtQuick 2.3 

Text { 
    id: dockWidget 
    text: "Waiting for a click..." 
} 

をしかし、あなたはその助けをどうするか、と言う、待つ:ここだけテキスト要素が、この信号の消費者は、何がこのようになりますことができます。さて、これまでのところ接続はありません。受信者アイテムをインスタンス化するときにそれを行います。そこでここでは、main.qmlファイルは次のようになります。

import QtQuick 2.3 
import QtQuick.Window 2.2 

Window { 
    visible: true 

    // Instantiate the ToolbarButton that emits the signal when clicked 
    ToolbarButton { 
     id: toolbarButton 
     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.topMargin: 10 
     anchors.leftMargin: 10 
    } 

    // Instantiate the DockWidget (just a Text element in this example) 
    DockWidget { 
     id: dockWidget 
     anchors.bottom: parent.bottom 
     anchors.bottomMargin: 10 
     anchors.left: parent.left 
     anchors.leftMargin: 10 

     // Complete the plumbing that connects the signal from item above 
     // with id: toolbarButton. 
     Connections { 
      target: toolbarButton 
      // When signal addSaveHistory is emitted, 
      // replace binding above with a new one 
      onAddSaveHistory: dockWidget.text = "Button was clicked" 
     } 
    } 
} 

実際にはDockWidgetの子であるとの接続要素は必要ありません。 ToolbarButtonとDockウィジェットは、これらのアイテムについてのより高いレベルの知識と相互作用するべき方法で一緒につながっているレゴのレンガであると考えてください。あなたが望むのであれば、これをより複雑な独自のQMLコンポーネントにまとめることができます。さらに

、あなたは一つだけ信号を気にしている場合、あなたも、すべての接続要素を必要としません:

import QtQuick 2.3 
import QtQuick.Window 2.2 

Window { 
    visible: true 

    ToolbarButton { 
     id: toolbarButton 
     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.topMargin: 10 
     anchors.leftMargin: 10 

     // Add signal handler directly to emitter 
     onAddSaveHistory: dockWidget.text = "Button was clicked" 
    } 

    DockWidget { 
     id: dockWidget 
     anchors.bottom: parent.bottom 
     anchors.bottomMargin: 10 
     anchors.left: parent.left 
     anchors.leftMargin: 10 
    } 
} 

・ホープ、このことができます。

+0

こんにちは、ショーン、 ありがとうございました。 Qmlを使用してインターフェイスを作成する別のアプリケーション用のプラグインを作成するためにqmlを使用しているため、私は固有の問題があります。私はmain.qmlを設定することはできません。これは私のmain.qmlをフォーマットする必要があります。元の投稿を編集してmain.qmlを表示しました。 – WesM

+0

私はToolbarButton.qmlを提案したように編集し、矩形アイテムの下に配置し、id.signalFucntion()構文を使ってシグナルを呼び出します。 しかし、私が確立しようとしている接続は、ToolbarButton.qmlの信号についてはわかりません。 – WesM

+0

alg.ui.addToolBarWidget()とalg.ui.addDockWidget()が何を言っているのかわかりません解決策は何ですか?私は引数として渡されたQMLファイルをインスタンス化すると推測しています。プラグイン{}スコープでConnections {}要素を使用できるようにするには、どういうわけかそれらを参照できる必要があります。これらの関数のドキュメントを見る必要があるかもしれません(私はまだそれらのための何かを見つけることができません)か、それらを書いた開発者が典型的な例を持っているかどうか尋ねる必要があります。 –

関連する問題