2017-08-10 7 views
0

長方形の幅と高さを取得する必要がありますComponent.OnCompletedハンドラですが、同じものを印刷する場合は、 :QML - "完了時"の幅、高さなどを取得できません

[EDIT-1] - コードを追加しました。

import QtQuick 2.6 
import QtQuick.Controls 2.2 
import QtQuick.Window 2.3 

ApplicationWindow { 
    id: appWindow 
    visible: true 
    width: 600 
    height: 400 
    title: qsTr("test") 
    flags: Qt.Window | Qt.FramelessWindowHint 

    Rectangle{ 
     id:rectParent 
     width:parent.width * 0.75 
     height: parent.height * 0.70 
     Rectangle{ 
      id:rectChild 
      width:parent.width * 0.75 
      height: parent.height * 0.70 
      Component.onCompleted: { 
       console.log("Width=",width) //prints "0" . 
      } 
     } 
    } 
} 

どのように幅、高さを取得するにはonCompleted

+0

なぜあなたは 'Component.onCompleted'でそれを必要としますか? 'onWidthChanged'ではできませんか? – GrecKo

+0

サイズを変更した後に再び表示されます。 – derM

+0

最初の非ゼロ幅を維持することができます。または、再度サイズを変更する際にそれを取得する必要があります。私は、なぜpra7が非宣言型コードを使用する必要があるのか​​を尋ねました。 – GrecKo

答えて

1

[OK]を、私は再び起動してみましょう:
あなたの問題はparent背後を隠して何、誤解です。

import QtQuick 2.6 
import QtQuick.Controls 2.0 
ApplicationWindow { 
    id: appWindow 
    width: 600 
    height: 400 
    visible: true 
    Rectangle { 
     id: someRect 
     width: parent.width * 0.7 
     heigth: parent.height * 0.7 
    } 
} 

ここでは、someRectためparentappWindow、及びそのための、parent.width = appWindow.width = 600である、と仮定します。 appWindowはタイプItemのないようこれは、

someRectの親がappWindowすることはできません間違っています。実際にはsomeRect.parent === appWindow.contentItemだからwidth: parent.width => width: appWindow.contentItem.widthです。

contentItemの幅は、作成時に0であり、作成後はappWindow.widthに再サイジングされるという問題があります。

これはappWindow.contentItemの幅が600にリサイズされるまでsomeRect.width0であることを、意味 - Component.onCompletedが実行されるまで起こらないであろう。最終値は、プロパティappWindow.widthに、最初から入手可能であるように

溶液は、appWindow.contentItemの幅依存性をショートカットすること、です。

の別の例を見てみましょう:幅のみとすぐappWindow.contentItemがリサイズされているように変化する一方、ここで

import QtQuick 2.6 
import QtQuick.Controls 2.0 
ApplicationWindow { 
    id: appWindow 
    width: 600 
    height: 400 
    visible: true 
    Rectangle { 
     id: someRect 
     width: parent.width * 0.7 // depends on appWindow.contentItem.width -> initally 0, changing soon after 
     heigth: appWindow.height * 0.7 // depends on appWindow.height -> initially 400. 
     Component.onCompleted: console.log('someRect:', width, height) // prints: "someRect: 0 280" 
     Rectangle { 
      id: someOtherRect 
      width: parent.width * 0.7 // depends on someRect.width which is initally 0 as it depends on appWindow.contentItem.width 
      height: parent.height * 0.7 // depends on someRect.height which is initally 400 * 0.7 
      Component.onCompleted: console.log('someOtherRect:', width, height) // prints "someOtherRect: 0, 196" 
     } 
    } 
} 

を、高さは、最初から設定されます。それで道に従う方が良いです、私はheightのために使用しました。

親がそうでないかもしれない多くのQMLコンポーネントがあります。カスタムコンポーネントの場合、たとえば次のようになります。 default property aliasを使用して "子"をネストされたItemにプッシュするすべてのコンポーネント

+0

追加中にいくつかのコードを見逃しました。私の質問を編集してください。 – pra7

+0

ありがとう。どのように幅を取​​得するには? 1つの方法は、onWidthChangedにコンポーネントを作成することです。これを行うための他の方法はありますか? – pra7

+0

ええ - @dtechと同じように、 'parent'の代わりに' id'を使います。私は最初から書いています。 – derM

2

アイテムのparentは、ウィンドウに直接ネストされたアイテムではなく、ウィンドウのcontentItemです。

が、この代わりに試してみてください:

Rectangle{ 
     id:rect 
     width: appWindow.width * 0.75 
     height: appWindow.height * 0.70 
} 

同じことがあなたの「完全なコード」に適用されます。親が最初の長方形になりますので、あなたは、第二の長方形でparentを使用することができます

Rectangle{ 
     id:rectParent 
     width:appWindow.width * 0.75 
     height: appWindow.height * 0.70 
     Rectangle{ 
      id:rectChild 
      width:parent.width * 0.75 
      height: parent.height * 0.70 
      Component.onCompleted: { 
       console.log("Width=",width) //prints "Width= 337.5" 
      } 
     } 
    } 

、最初のものはウィンドウ内にネストされているので、プロパティのサイズを取得するには、親ではなくウィンドウを参照する必要があります。

+0

変更はありません。私は0を得ています。 – pra7

+1

不可能、それはそのように動作するはずです。 Buildを実行してください。qmakeを実行すると、変更内容が反映されていない可能性があります。私は 'qml:Width = 450'を表示します。 – dtech

+0

申し訳ありませんが質問に追加中にいくつかのコードを忘れました。私は編集してくださいを確認してください。 – pra7

関連する問題