当前位置:   article > 正文

QT Quick(QML)学习笔记1_qml rectangle scrollview

qml rectangle scrollview

1、添加网络模块 

目的:是为了使得Image能够加载到http或https(https的访问强查看:https://blog.csdn.net/wtl1992/article/details/110401820

2、QT QML的Hello,world

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Rectangle{
  9. id: rect
  10. width: parent.width / 2
  11. height: parent.height / 2
  12. color: "#FF0000"
  13. anchors.centerIn: parent
  14. }
  15. }

3、Image

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Rectangle{
  9. id: rect
  10. width: parent.width / 2
  11. height: parent.height / 2
  12. color: "#FF0000"
  13. anchors.centerIn: parent
  14. Image {
  15. id: image
  16. fillMode: Image.PreserveAspectFit
  17. source: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2076212417,3005667226&fm=26&gp=0.jpg"
  18. }
  19. }
  20. }

4、ScrollView

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. Window {
  5. id: window
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10. //构造12个Rectangle
  11. ScrollView{
  12. id: scrollView
  13. width: window.width
  14. height: window.height
  15. ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
  16. ScrollBar.vertical.policy: ScrollBar.AlwaysOn
  17. Flow{
  18. width: scrollView.width
  19. anchors.margins: 4
  20. spacing: 10
  21. Repeater{
  22. model: 12
  23. Rectangle{
  24. width: 200
  25. height: 200
  26. color: "blue"
  27. }
  28. }
  29. }
  30. }
  31. }

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. Window {
  5. id: window
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10. //构造12个Rectangle
  11. ScrollView{
  12. id: scrollView
  13. width: window.width
  14. height: window.height
  15. ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
  16. ScrollBar.vertical.policy: ScrollBar.AlwaysOn
  17. Flow{
  18. width: scrollView.width
  19. anchors.margins: 4
  20. spacing: 10
  21. Repeater{
  22. model: 12
  23. Image{
  24. width: 200
  25. fillMode: Image.PreserveAspectFit
  26. source: "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3353166494,2700282750&fm=26&gp=0.jpg"
  27. }
  28. }
  29. }
  30. }
  31. }

5、自定义组件(Rectangle+TextInput)

TextInput:

MTextInput.qml:

  1. import QtQuick 2.0
  2. Rectangle{
  3. id: rect
  4. property int textInputWidth: 200
  5. property int textInputHeight: 30
  6. property string textInputColor: qsTr("#000000")
  7. property string background: qsTr("#CCCCCC")
  8. property string fontFamily: qsTr("微软雅黑")
  9. property int fontSize: 20
  10. property int borderWidth: 1
  11. property string borderColor: qsTr("#000000")
  12. property alias anchorBottom: rect.bottom
  13. property alias anchorLeft: rect.left
  14. property alias textInputText: textinput.text
  15. signal textchanged
  16. width: textInputWidth
  17. height: textInputHeight
  18. color: background
  19. border.width: borderWidth
  20. border.color: borderColor
  21. anchors.centerIn: parent
  22. TextInput{
  23. id: textinput
  24. width: textInputWidth
  25. height: textInputHeight
  26. color: textInputColor
  27. font.family: fontFamily
  28. font.pixelSize: fontSize
  29. verticalAlignment: TextInput.AlignVCenter
  30. onTextChanged: {
  31. rect.textchanged();
  32. }
  33. }
  34. }

main.qml:

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. Window {
  5. id: window
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10. MTextInput{
  11. id: mtextinput
  12. textInputColor: qsTr("#FF0000")
  13. onTextchanged: {
  14. text.text = mtextinput.textInputText;
  15. }
  16. }
  17. Rectangle{
  18. id: rect
  19. color: qsTr("#FF0000")
  20. width: mtextinput.textInputWidth
  21. height: mtextinput.textInputHeight
  22. anchors.top: mtextinput.anchorBottom
  23. anchors.left: mtextinput.anchorLeft
  24. anchors.topMargin: 10
  25. Text{
  26. id: text
  27. width: parent.width
  28. height: parent.height
  29. }
  30. }
  31. }

6、Keys

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. Window {
  5. id: window
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10. Rectangle{
  11. id: rect
  12. width: parent.width
  13. height: parent.height
  14. color: qsTr("yellow")
  15. anchors.centerIn: window
  16. focus: true
  17. Keys.onPressed: {
  18. console.log(event.key);
  19. }
  20. }
  21. }

7、MouseArea

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. Window {
  5. id: window
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10. Rectangle{
  11. id: rect
  12. width: parent.width / 2
  13. height: parent.height / 2
  14. color: qsTr("yellow")
  15. anchors.centerIn: parent
  16. focus: true
  17. MouseArea{
  18. id: mouseArea
  19. width: parent.width
  20. height: parent.height
  21. hoverEnabled: true
  22. onEntered: {
  23. console.log("鼠标进入");
  24. rect.color = "blue";
  25. }
  26. onExited: {
  27. console.log("鼠标离开");
  28. rect.color = "yellow";
  29. }
  30. }
  31. }
  32. }

8、QML请求数据

Ajax.js:

  1. // GET
  2. function get(url, success, failure)
  3. {
  4. let xhr = new XMLHttpRequest;
  5. xhr.open("GET", url);
  6. xhr.onreadystatechange = function() {
  7. handleResponse(xhr, success, failure);
  8. }
  9. xhr.send();
  10. }
  11. // POST
  12. function post(url, arg, success, failure)
  13. {
  14. let xhr = new XMLHttpRequest;
  15. xhr.open("POST", url);
  16. xhr.setRequestHeader("Content-Length", arg.length);
  17. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //用POST的时候一定要有这句
  18. xhr.onreadystatechange = function() {
  19. handleResponse(xhr, success, failure);
  20. }
  21. xhr.send(arg);
  22. }
  23. // 处理返回值
  24. function handleResponse(xhr, success, failure){
  25. if (xhr.readyState === XMLHttpRequest.DONE) {
  26. if (xhr.status === 200){
  27. if (success !== null && success !== undefined)
  28. {
  29. let result = xhr.responseText;
  30. try{
  31. success(result, JSON.parse(result));
  32. }catch(e){
  33. success(result, {});
  34. }
  35. }
  36. }
  37. else{
  38. if (failure !== null && failure !== undefined)
  39. failure(xhr.responseText, xhr.status);
  40. }
  41. }
  42. }

Component的加载事件:

  1. Component.onCompleted: {
  2. Ajax.get("https://ljxwtl.cn/getPagingNewZongYis?pageIndex=1",
  3. function(result, data){
  4. console.log(JSON.stringify(data));
  5. listView.model = data;
  6. },function(){
  7. });
  8. }

将Ajax请求的数据绑定到ListView上

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. import QtQml.Models 2.14
  5. import "javascript/Ajax.js" as Ajax
  6. Window {
  7. id: window
  8. visible: true
  9. width: 640
  10. height: 480
  11. title: qsTr("Hello World")
  12. ListView{
  13. id: listView
  14. width: parent.width - 40
  15. height: parent.height
  16. spacing: 10
  17. anchors.centerIn: parent
  18. delegate: Rectangle{
  19. width: parent.width
  20. height: 30
  21. color: "yellow"
  22. Text{
  23. width: parent.width
  24. height: 30
  25. text: modelData.title
  26. horizontalAlignment: Text.AlignHCenter
  27. verticalAlignment: Text.AlignVCenter
  28. font.family: "微软雅黑"
  29. font.pixelSize: 20
  30. }
  31. }
  32. }
  33. Component.onCompleted: {
  34. Ajax.get("https://ljxwtl.cn/getPagingNewZongYis?pageIndex=1",
  35. function(result, data){
  36. console.log(JSON.stringify(data));
  37. listView.model = data;
  38. },function(){
  39. });
  40. }
  41. }

外部嵌套ScrollView:

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. import QtQml.Models 2.14
  5. import "javascript/Ajax.js" as Ajax
  6. Window {
  7. id: window
  8. visible: true
  9. width: 640
  10. height: 480
  11. title: qsTr("Hello World")
  12. ScrollView{
  13. id: scrollView
  14. width: parent.width
  15. height: parent.height
  16. GridView{
  17. id:gridView
  18. width: parent.width - 40
  19. cellWidth: 300
  20. cellHeight: 360
  21. delegate: Rectangle{
  22. width: 300
  23. height: 360
  24. border.width: 1
  25. border.color: "#000000"
  26. Text{
  27. id: text
  28. width: parent.width
  29. height: 50
  30. text: modelData.fromPageTitle
  31. horizontalAlignment: Text.AlignHCenter
  32. verticalAlignment: Text.AlignVCenter
  33. font.family: "微软雅黑"
  34. font.pixelSize: 16
  35. wrapMode: TextEdit.WrapAnywhere
  36. }
  37. Image{
  38. width: parent.width
  39. height: parent.height - 50
  40. source: modelData.middleURL
  41. fillMode: Image.PreserveAspectFit
  42. anchors.top: text.bottom
  43. horizontalAlignment: Image.AlignHCenter
  44. }
  45. }
  46. }
  47. }
  48. Component.onCompleted: {
  49. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=1&pageSize=60",
  50. function(result, data){
  51. console.log(JSON.stringify(data));
  52. gridView.model = data;
  53. },function(){
  54. });
  55. }
  56. }

ScrollView里的ScrollBar的事件监听:

  1. ScrollBar.vertical.onPositionChanged: {
  2. let scrollBarHeight = ScrollBar.vertical.visualSize * 1000;
  3. let scrollBarPosition = ScrollBar.vertical.position * 1000;
  4. if (scrollBarPosition + scrollBarHeight > scrollView.height * 0.9){
  5. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=1&pageSize=60",
  6. function(result, data){
  7. console.log("JSON.stringify(data)");
  8. //gridView.model = data;
  9. },function(){
  10. });
  11. }
  12. }
  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. import QtQml.Models 2.14
  5. import "javascript/Ajax.js" as Ajax
  6. Window {
  7. id: window
  8. visible: true
  9. width: 640
  10. height: 480
  11. title: qsTr("Hello World")
  12. ScrollView{
  13. id: scrollView
  14. width: parent.width
  15. height: parent.height
  16. //Component.onCompleted: ScrollBar.vertical.position = 1.0
  17. ScrollBar.vertical.onPositionChanged: {
  18. let scrollBarHeight = ScrollBar.vertical.visualSize * 1000;
  19. let scrollBarPosition = ScrollBar.vertical.position * 1000;
  20. if (scrollBarPosition + scrollBarHeight > scrollView.height * 0.9){
  21. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=1&pageSize=60",
  22. function(result, data){
  23. console.log("JSON.stringify(data)");
  24. //gridView.model = data;
  25. },function(){
  26. });
  27. }
  28. }
  29. GridView{
  30. id:gridView
  31. width: parent.width - 40
  32. cellWidth: 300
  33. cellHeight: 360
  34. delegate: Rectangle{
  35. width: 300
  36. height: 360
  37. border.width: 1
  38. border.color: "#000000"
  39. Text{
  40. id: text
  41. width: parent.width
  42. height: 50
  43. text: modelData.fromPageTitle
  44. horizontalAlignment: Text.AlignHCenter
  45. verticalAlignment: Text.AlignVCenter
  46. font.family: "微软雅黑"
  47. font.pixelSize: 16
  48. wrapMode: TextEdit.WrapAnywhere
  49. }
  50. Image{
  51. width: parent.width
  52. height: parent.height - 50
  53. source: modelData.middleURL
  54. fillMode: Image.PreserveAspectFit
  55. anchors.top: text.bottom
  56. horizontalAlignment: Image.AlignHCenter
  57. }
  58. }
  59. }
  60. }
  61. Component.onCompleted: {
  62. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=1&pageSize=60",
  63. function(result, data){
  64. console.log(JSON.stringify(data));
  65. gridView.model = data;
  66. },function(){
  67. });
  68. }
  69. }

动态添加新数据:

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. import QtQuick.Controls 2.14
  4. import QtQml.Models 2.14
  5. import "javascript/Ajax.js" as Ajax
  6. Window {
  7. id: window
  8. visible: true
  9. width: 640
  10. height: 480
  11. title: qsTr("Hello World")
  12. function setTimeout(callback,timeout){
  13. let timer = Qt.createQmlObject("import QtQuick 2.14; Timer {}", window);
  14. timer.interval = timeout;
  15. timer.repeat = false;
  16. timer.triggered.connect(callback);
  17. timer.start();
  18. }
  19. function setInterval(callback,timeout){
  20. let timer = Qt.createQmlObject("import QtQuick 2.14; Timer {}", window);
  21. timer.interval = timeout;
  22. timer.repeat = true;
  23. timer.triggered.connect(callback);
  24. timer.start();
  25. }
  26. ListModel{
  27. id:listModel
  28. }
  29. ScrollView{
  30. id: scrollView
  31. width: parent.width
  32. height: parent.height
  33. property int pageIndex: 2
  34. property bool flag: true
  35. //Component.onCompleted: ScrollBar.vertical.position = 1.0
  36. ScrollBar.vertical.onPositionChanged: {
  37. let scrollBarSize = ScrollBar.vertical.size * 1000;
  38. let scrollBarHeight = ScrollBar.vertical.height;
  39. let scrollBarPosition = ScrollBar.vertical.position;
  40. // console.log(scrollBarHeight * scrollBarPosition + scrollBarSize);
  41. // console.log(scrollView.height);
  42. // console.log(flag);
  43. if (scrollBarHeight * scrollBarPosition + scrollBarSize > scrollView.height * 0.9 && flag){
  44. flag = false;
  45. pageIndex ++;
  46. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=" + pageIndex + "&pageSize=60",
  47. function(result, data){
  48. for (let i=0;i<data.length;i++){
  49. listModel.append(data[i]);
  50. }
  51. setTimeout(function(){
  52. flag = true;
  53. console.log("--------------------------------")
  54. },1000);
  55. },function(){
  56. });
  57. }
  58. }
  59. GridView{
  60. id:gridView
  61. width: parent.width - 40
  62. cellWidth: 300
  63. cellHeight: 360
  64. model: listModel
  65. delegate: Rectangle{
  66. width: 300
  67. height: 360
  68. border.width: 1
  69. border.color: "#000000"
  70. Text{
  71. id: text
  72. width: parent.width
  73. height: 50
  74. text: fromPageTitle
  75. horizontalAlignment: Text.AlignHCenter
  76. verticalAlignment: Text.AlignVCenter
  77. font.family: "微软雅黑"
  78. font.pixelSize: 16
  79. wrapMode: TextEdit.WrapAnywhere
  80. }
  81. Image{
  82. width: parent.width
  83. height: parent.height - 50
  84. source: middleURL
  85. fillMode: Image.PreserveAspectFit
  86. anchors.top: text.bottom
  87. horizontalAlignment: Image.AlignHCenter
  88. }
  89. }
  90. }
  91. }
  92. Component.onCompleted: {
  93. Ajax.get("https://ljxwtl.cn/getAllMatchingImages?keyword=%E7%BE%8E%E5%A5%B3&pageIndex=1&pageSize=60",
  94. function(result, data){
  95. console.log(JSON.stringify(data));
  96. for (let i=0;i<data.length;i++){
  97. listModel.append(data[i]);
  98. }
  99. },function(){
  100. });
  101. }
  102. }

9、QML与C++交互

  1. //设置rootContext的变量
  2. QQmlContext * context = engine.rootContext();
  3. context->setContextProperty("name","王天龙");
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlContext>
  4. int main(int argc, char *argv[])
  5. {
  6. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  7. QGuiApplication app(argc, argv);
  8. QQmlApplicationEngine engine;
  9. //设置rootContext的变量
  10. QQmlContext * context = engine.rootContext();
  11. context->setContextProperty("name","王天龙");
  12. const QUrl url(QStringLiteral("qrc:/main.qml"));
  13. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  14. &app, [url](QObject *obj, const QUrl &objUrl) {
  15. if (!obj && url == objUrl)
  16. QCoreApplication::exit(-1);
  17. }, Qt::QueuedConnection);
  18. engine.load(url);
  19. return app.exec();
  20. }
  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Rectangle{
  9. id: rect
  10. width: parent.width / 2
  11. height: parent.height / 2
  12. color: "#000000"
  13. anchors.centerIn: parent
  14. Text{
  15. id: text
  16. horizontalAlignment: Text.AlignHCenter
  17. verticalAlignment: Text.AlignVCenter
  18. anchors.centerIn: parent
  19. text: name
  20. color: "#FFFFFF"
  21. font.pixelSize: 18
  22. font.family: "微软雅黑"
  23. }
  24. }
  25. }

10、Loader的使用

10.1、source属性

Rect.qml:

  1. import QtQuick 2.14
  2. Rectangle{
  3. id: rect
  4. width: parent.width / 2
  5. height: parent.height / 2
  6. color: "#000000"
  7. anchors.centerIn: parent
  8. Text{
  9. id: text
  10. horizontalAlignment: Text.AlignHCenter
  11. verticalAlignment: Text.AlignVCenter
  12. anchors.centerIn: parent
  13. text: name
  14. color: "#FFFFFF"
  15. font.pixelSize: 18
  16. font.family: "微软雅黑"
  17. }
  18. }
  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Loader{
  9. id: loader
  10. anchors.fill: parent
  11. source: Qt.resolvedUrl("Rect.qml")
  12. }
  13. }

10.2、sourceComponent

10.2.1、在同一个qml文件里定义

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Loader{
  9. id: loader
  10. anchors.fill: parent
  11. sourceComponent: component
  12. }
  13. Component{
  14. id: component
  15. Rectangle{
  16. id: rect
  17. width: parent.width / 2
  18. height: parent.height / 2
  19. color: "#000000"
  20. anchors.centerIn: parent
  21. Text{
  22. id: text
  23. horizontalAlignment: Text.AlignHCenter
  24. verticalAlignment: Text.AlignVCenter
  25. anchors.centerIn: parent
  26. text: name
  27. color: "#FFFFFF"
  28. font.pixelSize: 18
  29. font.family: "微软雅黑"
  30. }
  31. }
  32. }
  33. }

10.2.2、在不同文件里加载

Component.qml:

  1. import QtQuick 2.14
  2. Rectangle{
  3. id: rect
  4. width: 300
  5. height: 300
  6. color: "#000000"
  7. Text{
  8. id: text
  9. horizontalAlignment: Text.AlignHCenter
  10. verticalAlignment: Text.AlignVCenter
  11. anchors.centerIn: parent
  12. text: name
  13. color: "#FFFFFF"
  14. font.pixelSize: 18
  15. font.family: "微软雅黑"
  16. }
  17. }
  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. property var component
  9. Loader{
  10. id: loader
  11. anchors.fill: parent
  12. sourceComponent: component
  13. }
  14. Component.onCompleted: {
  15. component = Qt.createComponent("Component.qml");
  16. console.log(component)
  17. }
  18. }

10.3、Qt.createComponent("Component.qml")

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. property var component
  9. Loader{
  10. id: loader
  11. anchors.fill: parent
  12. sourceComponent: component
  13. }
  14. Component.onCompleted: {
  15. component = Qt.createComponent("Component.qml");
  16. console.log(component)
  17. }
  18. }

10.4、Qt.createQmlObject("import QtQuick 2.14;Component{Rectangle{color:'blue';width:200;height:200}}",window);

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. property var component
  10. Loader{
  11. id: loader
  12. anchors.fill: parent
  13. sourceComponent: component
  14. }
  15. Component.onCompleted: {
  16. component = Qt.createQmlObject("import QtQuick 2.14;Component{Rectangle{color:'blue';width:200;height:200}}",window);
  17. console.log(component)
  18. }
  19. }

11、Animation动画

11.1、NumberAnimation

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. property var component
  10. Rectangle{
  11. id: rect
  12. width: parent.width / 2
  13. height: parent.height / 2
  14. color: "#000000"
  15. Text{
  16. id: text
  17. horizontalAlignment: Text.AlignHCenter
  18. verticalAlignment: Text.AlignVCenter
  19. anchors.centerIn: parent
  20. text: name
  21. color: "#FFFFFF"
  22. font.pixelSize: 18
  23. font.family: "微软雅黑"
  24. }
  25. NumberAnimation{
  26. id: numberAnimation
  27. target: rect
  28. property: "scale"
  29. from: 0
  30. to: 1
  31. duration: 1000
  32. running: true
  33. }
  34. }
  35. }

11.2、PropertyAnimation

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. property var component
  10. Rectangle{
  11. id: rect
  12. width: parent.width / 2
  13. height: parent.height / 2
  14. color: "#000000"
  15. Text{
  16. id: text
  17. horizontalAlignment: Text.AlignHCenter
  18. verticalAlignment: Text.AlignVCenter
  19. anchors.centerIn: parent
  20. text: name
  21. color: "#FFFFFF"
  22. font.pixelSize: 18
  23. font.family: "微软雅黑"
  24. }
  25. PropertyAnimation {
  26. id: numberAnimation
  27. target: rect
  28. property: "scale"
  29. from: 0
  30. to: 1
  31. duration: 1000
  32. running: true
  33. }
  34. }
  35. }

11.3、SequentialAnimation

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. property var component
  10. Rectangle{
  11. id: rect
  12. width: parent.width / 2
  13. height: parent.height / 2
  14. color: "#000000"
  15. Text{
  16. id: text
  17. horizontalAlignment: Text.AlignHCenter
  18. verticalAlignment: Text.AlignVCenter
  19. anchors.centerIn: parent
  20. text: name
  21. color: "#FFFFFF"
  22. font.pixelSize: 18
  23. font.family: "微软雅黑"
  24. }
  25. SequentialAnimation{
  26. running: true
  27. PropertyAnimation {
  28. id: numberAnimation1
  29. target: rect
  30. property: "scale"
  31. from: 0
  32. to: 1
  33. duration: 1000
  34. }
  35. PropertyAnimation {
  36. id: numberAnimation2
  37. target: rect
  38. property: "opacity"
  39. from: 0
  40. to: 1
  41. duration: 1000
  42. }
  43. }
  44. }
  45. }

11.4、ParallelAnimation

  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. property var component
  10. Rectangle{
  11. id: rect
  12. width: parent.width / 2
  13. height: parent.height / 2
  14. color: "#000000"
  15. Text{
  16. id: text
  17. horizontalAlignment: Text.AlignHCenter
  18. verticalAlignment: Text.AlignVCenter
  19. anchors.centerIn: parent
  20. text: name
  21. color: "#FFFFFF"
  22. font.pixelSize: 18
  23. font.family: "微软雅黑"
  24. }
  25. ParallelAnimation{
  26. running: true
  27. PropertyAnimation {
  28. id: numberAnimation1
  29. target: rect
  30. property: "scale"
  31. from: 0
  32. to: 1
  33. duration: 1000
  34. }
  35. PropertyAnimation {
  36. id: numberAnimation2
  37. target: rect
  38. property: "opacity"
  39. from: 0
  40. to: 1
  41. duration: 1000
  42. }
  43. }
  44. }
  45. }

12、设置窗口为无边框

flags: Qt.FramelessWindowHint
  1. import QtQuick 2.14
  2. import QtQuick.Window 2.14
  3. Window {
  4. id: window
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Hello World")
  9. flags: Qt.FramelessWindowHint
  10. property var component
  11. Rectangle{
  12. id: rect
  13. width: parent.width / 2
  14. height: parent.height / 2
  15. color: "#000000"
  16. Text{
  17. id: text
  18. horizontalAlignment: Text.AlignHCenter
  19. verticalAlignment: Text.AlignVCenter
  20. anchors.centerIn: parent
  21. text: name
  22. color: "#FFFFFF"
  23. font.pixelSize: 18
  24. font.family: "微软雅黑"
  25. }
  26. ParallelAnimation{
  27. running: true
  28. PropertyAnimation {
  29. id: numberAnimation1
  30. target: rect
  31. property: "scale"
  32. from: 0
  33. to: 1
  34. duration: 1000
  35. }
  36. PropertyAnimation {
  37. id: numberAnimation2
  38. target: rect
  39. property: "opacity"
  40. from: 0
  41. to: 1
  42. duration: 1000
  43. }
  44. }
  45. }
  46. }

13、背景动画

  1. Rectangle{
  2. id: rect
  3. width: parent.width / 2
  4. height: parent.height / 2
  5. color: "blue"
  6. anchors.centerIn: parent
  7. PropertyAnimation{
  8. id: animation
  9. target: rect
  10. property: "color"
  11. from: "blue"
  12. to: "yellow"
  13. duration: 2000
  14. running: true
  15. }
  16. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/117405
推荐阅读
相关标签
  

闽ICP备14008679号