Qt for Quick
程序员文章站
2024-01-05 17:36:40
...
Bindings-绑定
一个属性的变化会同时让所有的属性变化
SpinBox {
id: spinbox
}
Rectangle {
color: {
switch(spinbox.value) {
case 0:
return "red";
case 1:
return "green";
...
}
}
}
可自定义控件
MyComponent.qml
Item {
property string myProperty: "Hello"
readonly property int myOtherProperty: 1
signal somethingChanged
function doSomething() {
print("I did something");
}
Rectangle {...}
}
与C++的交互
C++ 通过注册信号及方法到Main.cpp中,既可以把接口暴露给qml端,qml可无缝调用
# MyClass.h
class MyClass : public QObject {
Q_OBJECT
Q_PROPERTY(int someProperty...)
singnals:
void mySignal();
public:
Q_INVOKABLE void doSomething();
}
# main.cpp
MyClass obj;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myObject",&obj);
#main.qml
Button {
text: myObject.someProperty
onClicked: myObject.doSomething()
}
布局
- 基于像素位置的布局
- 动态布局
- Column / Row Layouts
- Grid Layout
- 铆接Anchors
- 定位器Positioners(only manage positions)
+ column
+ Row
+ Flow
+ Grid