小编Mit*_*tch的帖子

清除QML锚点

我有一个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 position qml qt-quick

22
推荐指数
1
解决办法
1万
查看次数

在拖放项目期间滚动项目

按照这个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)

qt drag-and-drop drag qml qtquick2

7
推荐指数
1
解决办法
1789
查看次数

使用依赖注入作为单身人士的替代

我一直都知道Singletons是"糟糕的",但直到现在我已经从C++转向Java,我决定找到解决方法.从一点阅读,我发现工厂或依赖注入可能会完成这项工作,但我想对此进行一些确认.

作为一个例子,我即将编写一个可以存储的AnimationCache单例Map<String, Animation>.不同的类应该能够(基本上)在任何地方访问该类,以便他们可以轻松有效地加载动画.使用DI的等效代码看起来很简单的一个非常简短的例子.

另外,Guice是非网络应用程序DI的良好框架吗?我使用Spring进行Web开发,但我不太确定它是否适用于游戏.

java singleton dependency-injection guice

6
推荐指数
2
解决办法
2941
查看次数

在GSON中反序列化接口

想不出更好的措辞这个问题,但基本上我想在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 …

java json interface class gson

6
推荐指数
1
解决办法
8683
查看次数

在拖放时将QGraphicsItems与网格对齐

例如,如果我想使用a在游戏中显示玩家的库存,QGraphicsView我怎么能强制执行基于网格的视图,其中拖放项目总是导致它们与网格对齐?

grid qt qgraphicsview qgraphicsscene qt5

6
推荐指数
1
解决办法
2471
查看次数

仅在通过脚本访问时才定义属性

我遇到一些奇怪的行为,其中一个属性可以直接通过访问QObjectproperty功能,而不是通过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)

javascript qt properties qtscript qobject

6
推荐指数
1
解决办法
226
查看次数

找到一条线和一条QPainterPath之间的交点

我正在尝试确定一个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 intersection line qgraphicsscene qt5

6
推荐指数
1
解决办法
6548
查看次数

使用Qt Quick创建可伸缩,有光泽/闪亮的按钮

我想用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)

qt button qml qt-quick qtquickcontrols

6
推荐指数
1
解决办法
3243
查看次数

如何在Qt中以编程方式调整分割器小部件的大小?

我使用QSplitter并排放置一些小部件.

作为用户,我可以通过拖动分割器来调整这些小部件的大小.

作为程序员,我不知道如何准确指定目前我想要的宽度和高度.

这是我的原始状态(通过不同的延伸调整). 原始状态

我尝试使用setFixedSize(),但在调用之后,用户不能再自行调整窗口小部件的大小(这绝对是正确的行为,因为大小得到'固定'). 在此输入图像描述

如果我使用resize(),它几乎没有效果.小部件调整大小,但(!)不正确和(!)当我再次开始拖动时,小部件获得其初始状态. 在此输入图像描述

有没有办法正确调整代码中左侧窗口小部件的大小?我不希望有固定的大小,但resize()无法正常工作,你可以看到.所以我该怎么做?

c++ qt resize qsplitter

6
推荐指数
1
解决办法
2195
查看次数

如何创建Qt-Quick测试

我必须创建一个单元测试.

但首先,我要弄明白该做什么.有一个QtQuick2-App编写,现在我想用GUI进行单元测试.使用GUI进行单元测试的步骤是什么?阅读Qt文档后,我无法创建任何从测试开始的想法.

希望有人可以帮助我.

编辑:我是能够运行一些测试,加入后tst_button.qmltst_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)

user-interface qttest qml qtquick2

6
推荐指数
1
解决办法
2956
查看次数