我已经得到了这个Projekt,它使用QStatemachine来管理UI,我想在其中添加一个自定义列表。UI应该只被关键事件操纵。据我所知,我需要一个qml方面的ListView。
ListView的委托只对鼠标输入或直接键输入作出反应。但是我在C++中使用了C++来操作它,因为它处理UI的所有关键事件。当我按右箭头键时,我想要发生的事情是将列表移到左边。
( currentItem总是在屏幕中央。)


所以我的ListView现在是这样的。
Component {
id:myDelegation
Item {
x: 50
width: 80
height: 60
Rectangle {
width: 60
height: 60
Text {
text: name
anchors.centerIn: parent
}
color: parent.ListView.isCurrentItem ? "red" : "steelblue";
scale: parent.ListView.isCurrentItem ? 1.5 : 1;
}
}
}
ListView {
id: listView1
x: 0
y: 50
width: 1920
height: 214
orientation: ListView.Horizontal
spacing: 4
model: TileList{}
delegate: myDelegation
preferredHighlightBegin: width / 2 - 10
preferredHighlightEnd: width / 2 + 10
highlightRangeMode: ListView.StrictlyEnforceRange
}c++ Statemachine是向qml发送信号的QStatemachine。
如何将信号绑定到Listview的委托?
发布于 2016-12-17 16:44:03
步骤一-将状态机公开为上下文属性,以便它对qml可见:
engine.rootContext()->setContextProperty("SM", stateMachinePtr);第二步-使用Connections元素建立连接:
Connections {
target: SM
onSomeSignal: doSomeStuff()
}发布于 2016-12-18 12:42:59
最简单的方法是将状态机设置为"currentIndex“
一个常见的模式是在QML和QStateMachine之间架设一个接口对象。
class StateInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(int currentIndex MEMBER m_currentIndex NOTIFY currentIndexChanged)
public:
explicit StateInterface(QObject *parent = 0);
signals:
void currentIndexChanged() const;
private:
int m_currentIndex;
};该对象的一个实例通过"context属性“机制公开给QML。
StateInterface stateInterface;
qmlEngine->rootContext()->setContextProperty("_stateInterface", &stateInterface);并根据需要在QML中使用。
ListView {
currentIndex: _stateInterface.currentIndex
}QStateMachine使用相同的stateInterface对象作为状态属性分配的目标。
QState *beginState = new QState(stateMachine);
beginState->assignProperty(&stateInterface, "currentIndex", 0);
// and so on.StateInterface对象还可以提供QML用来影响状态更改的槽。例如。
public slots:
void triggerReset() { emit trigger reset(); }
signals:
void reset();例如,QStateMachine可以通过将信号转换为beginState来对这些信号作出反应。
总结一下这个技巧:
QStateMachine控制应用程序状态。https://stackoverflow.com/questions/41200182
复制相似问题