无法显示按钮

时间:2014-04-27 16:22:05

标签: android qt qml

我正在尝试为Android制作某种fotboll应用程序。我现在面临的问题是,我需要在屏幕的一角制作一个按钮,(4种不同的),你可以移动游戏角色。但我现在已经制作了一个按钮,用于向上移动,但它没有出现在屏幕上。

主档案:

import QtQuick 2.0
import QtQuick.Window 2.1

Item {

    id:root

    width:Screen.width
    height:Screen.height-10
    focus:true

    Keys.onPressed: {

        if(event.key===Qt.Key_Up)
        {
            event.accepted = true;
            player.y=(player.y) - 40

        }
        if(event.Key === Qt.Key_Down){
            event.accepted = true;
            player.y = (player.y)+ 40
           }
        if (event.key === Qt.Key_Right)
         { event.accepted=true;
            player.x=(player.x)+40

        }
   if (event.key === Qt.Key_Left)
{event.accepted = true;
       player.x=(player.x) -40
   }

    }

Flickable {
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height*2
    contentWidth:Screen.width
    interactive:true
    boundsBehavior: Flickable.StopAtBounds
    contentY: Math.min(contentHeight-height, Math.max(0, player.y-height/2))
    contentX: Math.min(contentWidth-width, Math.max(0, player.x-width/2))
    Image{
        id: feild
        anchors.fill:parent
        source:"Namnlös.png"
        sourceSize.height:Screen.height*2
        sourceSize.width:Screen.width
        }

    Image {
        id: player
        source:"asd.png"
        x:Screen.width/2
        y:Screen.height/2
    }
}

}

按钮文件:

import QtQuick 2.0
import QtQuick.Window 2.1


Rectangle {
    id: simplebutton
    color: "grey"
    width: 100; height: 50

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

    MouseArea {
        id: buttonMouseArea
        anchors.fill: parent
        onClicked: console.log(buttonLabel.text + "clicked" )

    }
}


Rectangle {
    id: button
    property color buttonColor: "lightblue"
    property color onHoverColor: "gold"
    property color borderColor: "white"

    signal buttonClick()
    onButtonClick: {
        console.log(buttonLabel.text + " clicked" )

    }
    MouseArea {
        onClicked: buttonClick()
        hoverEnabled: true
        onEntered: parent.border.color = onHoverColor
        onExited: parent.border.color = borderColor
    }
    color: buttonMouseArea.pressed ? Qt.darker(buttonColor, 1.5) : buttonColor
}

1 个答案:

答案 0 :(得分:0)

两件事:

1)您需要将按钮添加到布局中!由于您没有将按钮添加到主文件中,因此不会显示。

2)您需要将Button文件拆分为两个单独的qml文件,因为每个文件只能有一个“root”项。