2017-05-09 6 views
0

イメージにマスクを適用して円形にするときはいつでも、UIエレメントのアラインメントが壊れます。イメージは、いくつかのピクセル数だけ右にオフセットされます。イメージブレイクアライメントにマスクを適用する

// main.qml 
import QtQuick 2.8 
import QtQuick.Controls 2.1 
import QtQuick.Layouts 1.1 

ApplicationWindow { 
    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Example") 

    Page1Form { 

    } 
} 

// Page1Form.qml 
import QtQuick 2.8 
import QtQuick.Layouts 1.1 
import QtQuick.Controls 2.1 

Item { 
    RowLayout { 
     id: playerRowLayout 

     Layout.alignment: Qt.AlignLeft 

     Layout.fillWidth: true 

     RoundedImage { 
      id: displayImage 

      width: 50 
      height: 50 

      Layout.preferredWidth: 50 
      Layout.preferredHeight: 50 

      source: "Images/DisplayPicture.jpeg" 

      sourceSize.width: width 
      sourceSize.height: height 
     } 

     Text { 
      id: playerText 
      text: qsTr("Hameer Abbasi (Pro)") 
      font.family: "Source Sans Pro" 
      font.pixelSize: 12 
     } 
    } 
} 

// RoundedImage.qml 
import QtQuick 2.8 
import QtGraphicalEffects 1.0 

Image { 
    id: img 
    property bool rounded: true 
    property bool adapt: true 

    layer.enabled: rounded 
    layer.effect: OpacityMask { 
     maskSource: Rectangle { 
      anchors.centerIn: parent 
      width: adapt ? img.width : Math.min(img.width, img.height) 
      height: adapt ? img.height : Math.min(img.width, img.height) 
      radius: Math.min(width, height)*0.5 
     } 
    } 
} 

Misaligned

私はそうのように、位置ずれが消え、ImageRoundedImageを変更します。私はOpacityMaskanchors.fill: imgまたはanchors.centerIn: imgを追加するときに、私が得る、また

Image no longer rounded

次の結果(あなたが見ることができるように、ミスアライメントは消えずに動きました):

Blank space in the wrong position

が動作するようには思えないことを唯一のものは、テキストボックスにanchors.right: displayImage.leftを設定し、何とかハックのように思えるし、私は右何とか何かをやっていないよように私は感じることです。誰かが問題がどこにあるのか、これを修正するための「適切な」方法は何かを理解するのに役立つことができますか?

+0

私は 'width'と' Layout.preferredWidth'(それぞれの高さ)を一緒に使うべきではないと思います。あなたのエフェクトの['sourceRect'](http://doc.qt.io/qt-5/qml-qtquick-item.html#layer.sourceRect-prop)の設定を検討してください。 – derM

+0

derM 'layer.sourceRect:img.childrenRect'を追加しても問題は解決されませんでした。あなたが意図したものなのかどうかは分かりません。 –

+0

そういうもの。ちょうどそれを試してみる時間を見つけました。それは本当に不思議です...私は分かりません - あなたはレイアウトのものを落としてアンカーと通常の 'Row'sの代わりに行くかもしれませんか? – derM

答えて

0

私はあなたに説明できませんなぜこのことが起こっているのですか?私にはバグのように見えます。

簡単に回避策があります。のすぐ内側にImageを入れないでください。 RoundedImage.qmlを次のように変更してください。

// RoundedImage.qml 
import QtQuick 2.7 
import QtGraphicalEffects 1.0 

Item { 
    id: img 
    property bool rounded: true 
    property bool adapt: true 
    property alias source: image.source 
    property alias sourceSize: image.sourceSize 
    Image { 
     id: image 

     width: img.width 
     height: img.height 
     layer.enabled: rounded 
     layer.effect: OpacityMask { 
      maskSource: Rectangle { 
       width: adapt ? img.width : Math.min(img.width, img.height) 
       height: adapt ? img.height : Math.min(img.width, img.height) 
       radius: Math.min(width, height)*0.5 
      } 
     } 
    } 
} 

奇妙な動作はなくなりました。

関連する問題