我有一个MouseArea,我想开始居中,然后按下上/下/左/右键后有一个绝对位置.我的问题是我不知道如何清除MouseArea上的锚点,以便我可以指定一个绝对位置:
import QtQuick 2.0
import QtQuick.Window 2.0
Window {
id: screen
width: 360
height: 360
visible: true
Rectangle {
anchors.fill: parent
states: [
State {
name: "moved"
AnchorChanges {
target: mouseArea
anchors.bottom: undefined
anchors.left: undefined
anchors.right: undefined
anchors.top: undefined
}
}
]
MouseArea {
id: mouseArea
anchors.centerIn: parent
width: 250
height: 250
focus: true
onClicked: console.log("clicked!")
onPositionChanged: console.log("position changed!")
function moveMouseArea(x, y) {
mouseArea.x += x;
mouseArea.y += y;
mouseArea.state = "moved";
mouseAreaPosText.text = 'Mouse area was moved... new pos: …
Run Code Online (Sandbox Code Playgroud) 按照这个Qt 教程,我编写了这个简单的代码。有一个水平线,上面ListView
有一些简单的彩色矩形作为模型项目的代表。
import QtQuick 2.5
import QtQuick.Window 2.0
import QtQml.Models 2.2
Window {
visible: true
width: 300
height: 120
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent;
ListView{
id: timeline
anchors.fill: parent
orientation: ListView.Horizontal
model: visualModel
delegate: timelineDelegate
moveDisplaced: Transition {
NumberAnimation{
properties: "x,y"
duration: 200
}
}
DelegateModel {
id: visualModel
model: timelineModel
delegate: timelineDelegate
}
Component {
id: timelineDelegate
MouseArea {
id: dragArea
width: 100; height: 100
property bool held: false
drag.target: held ? content …
Run Code Online (Sandbox Code Playgroud) 我一直都知道Singletons是"糟糕的",但直到现在我已经从C++转向Java,我决定找到解决方法.从一点阅读,我发现工厂或依赖注入可能会完成这项工作,但我想对此进行一些确认.
作为一个例子,我即将编写一个可以存储的AnimationCache单例Map<String, Animation>
.不同的类应该能够(基本上)在任何地方访问该类,以便他们可以轻松有效地加载动画.使用DI的等效代码看起来很简单的一个非常简短的例子.
另外,Guice是非网络应用程序DI的良好框架吗?我使用Spring进行Web开发,但我不太确定它是否适用于游戏.
想不出更好的措辞这个问题,但基本上我想在GSON中存储具体类的名称(参见"运动"):
{
"player" : {
"position" : { "x" : 300, "y" : 400 },
"scale" : 1,
"rotation" : 0,
"sprite" : {
"frames" : [ {
"fileName" : "data/plane.png"
} ],
"duration" : 1
}
},
"movementDelay" : { "elapsed" : 0, "started" : 0, "delay" : 150 },
"movement" : { "class" : "controller.TopDownGridMovement" }
}
Run Code Online (Sandbox Code Playgroud)
这是包含我要反序列化的Movement接口的类:
public class PlayerController {
private Player player;
private DelayTimer movementDelay;
private Movement movement;
public PlayerController() {
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
我写了一个Custom …
例如,如果我想使用a在游戏中显示玩家的库存,QGraphicsView
我怎么能强制执行基于网格的视图,其中拖放项目总是导致它们与网格对齐?
我遇到一些奇怪的行为,其中一个属性可以直接通过访问QObject
的property
功能,而不是通过JavaScript:
#include <QApplication>
#include <QDebug>
#include <QScriptEngine>
#include <QStringList>
class Item : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(int typeId READ typeId)
Q_PROPERTY(int usesLeft READ usesLeft)
Item() :
mTypeId(0),
mUsesLeft(-1)
{
}
Item(int typeId) :
mTypeId(typeId)
{
if (typeId != 0) {
mUsesLeft = 5;
}
}
Item(const Item &item) :
QObject(0)
{
*this = item;
}
~Item()
{
}
Item& operator=(const Item& rhs)
{
mTypeId = rhs.mTypeId;
mUsesLeft = rhs.mUsesLeft;
return *this;
}
int typeId() const …
Run Code Online (Sandbox Code Playgroud) 我正在尝试确定一个hitscan射弹的路径(基本上是一条线,但我在其QPainterPath
示例中表示为它)与我场景中的一个项目相交的点.我不知道是否有发现使用所提供的功能,这一点的方式QPainterPath
,QLineF
等等.下面的代码说明了什么,我试图做的:
#include <QtWidgets>
bool hit(const QPainterPath &projectilePath, QGraphicsScene *scene, QPointF &hitPos)
{
const QList<QGraphicsItem *> itemsInPath = scene->items(projectilePath, Qt::IntersectsItemBoundingRect);
if (!itemsInPath.isEmpty()) {
const QPointF projectileStartPos = projectilePath.elementAt(0);
float shortestDistance = std::numeric_limits<float>::max();
QGraphicsItem *closest = 0;
foreach (QGraphicsItem *item, itemsInPath) {
QPointF distanceAsPoint = item->pos() - projectileStartPos;
float distance = abs(distanceAsPoint.x() + distanceAsPoint.y());
if (distance < shortestDistance) {
shortestDistance = distance;
closest = item;
}
}
QPainterPath targetShape = closest->mapToScene(closest->shape());
// hitPos = /* the …
Run Code Online (Sandbox Code Playgroud) 我想用Qt Quick创建下面的光泽按钮(最好是纯QML,没有C++):
它需要是可扩展的,所以我不能使用PNG等.
我的代码到目前为止:
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
ApplicationWindow {
id: window
color: "#cccccc"
width: 200
height: 200
Button {
id: button
width: Math.min(window.width, window.height) - 20
height: width * 0.3
anchors.centerIn: parent
text: "Button"
style: ButtonStyle {
background: Rectangle {
gradient: Gradient {
GradientStop {
position: 0
color: "#bbffffff"
}
GradientStop {
position: 0.6
color: "#00c0f5"
}
}
border.color: "grey"
border.width: height * 0.05
radius: height / 5
}
label: Label {
text: button.text …
Run Code Online (Sandbox Code Playgroud) 我必须创建一个单元测试.
但首先,我要弄明白该做什么.有一个QtQuick2-App编写,现在我想用GUI进行单元测试.使用GUI进行单元测试的步骤是什么?阅读Qt文档后,我无法创建任何从测试开始的想法.
希望有人可以帮助我.
编辑:我是能够运行一些测试,加入后tst_button.qml
和tst_test.cpp
我的项目(main.cpp中是评论现在).这是正确的方法,还是应该为测试创建一个新项目?如果是,需要什么样的项目?最后一个问题:MainForm
例如,我是否需要建立我的按键?
tst_button.qml
import QtQuick 2.4
import QtTest 1.0
Rectangle{
id: myRec
property var myMainForm: null
TestCase{
name:"ButtonClick"
when:windowShown
function test_init(){
var createMyWindow = "import QtQuick 2.0; MainForm{id:myForm}"
var myMainForm = Qt.createQmlObject(createMyWindow,myRec)
myRec.myMainForm = myMainForm
}
}
}
Run Code Online (Sandbox Code Playgroud)
tst_test.cpp
#include <QtQuickTest/quicktest.h>
QUICK_TEST_MAIN(test)
Run Code Online (Sandbox Code Playgroud)