一步一步学Qt(Qt Quick)/qml 开发第五篇(第一个可视的应用)

现在我们开始仿照官网的demo做一个小东西。

开始之前先看效果图


android如下:

下面开始正文:

因为在开始搞这个demo的时候,我遇到一个问题,所以为了避免这个,我将从创建项目开始。

OK,废话不多说,第一步,打开你的QtCreator,选择 文件-新建文件或项目-应用程序-Qt Quick Application -选择

输入项目名称和路径,下一步 


这一步需要注意的是,选择,我第一次创建的时候选择了Qt Quick2.0,但是项目中用到了Controls,虽然我在项目中引入了包,但是运行的时候一直没有出效果,而且会报引入出错的问题,然后我再重新创建一个项目,引入这个,就OK了,大家看需求弄,不过我这里是需要Controls的,所以选Qt Quick Controls 1.0 


然后下一步,选android的时候根据版本来,桌面也勾选,毕竟测试用的 


根据需求完成后点击完成,下面就是我们的项目 


然后,选中qml/yourProject文件夹,右击,在Explorer中显示 


然后在打开的文件夹中创建一个文件夹content和一个images文件夹,把你的图片资源拷贝到images文件夹下

然后看看你的QtCreator里面的项目结构


双击main.qml,清除里面的代码

然后在里面输入如下代码

//main.qml
import QtQuick 2.1
import QtQuick.Controls 1.0
import "content"//因为使用了文件目录,所以要引入
//使用ApplicationWindow前,必须引入QtQuick.Controls包
//ApplicationWindow提供了一个顶级应用程序窗口。
/**
  * ApplicationWindow里面有3个子元素分别为toolBar,statusBar,menuBar
  * 从子元素的内容可以知道,它是为了方便界面功能块划分的
  * 详细请参照API
  */
ApplicationWindow {
  width: 360
  height: 600
  //在界面定义一个矩形,颜色黑色,大小为填满父组件
  Rectangle {
    color: "#212126"
    anchors.fill: parent
  }
  //实现返回功能
  //监听按钮方法
  Keys.onReleased: {
    //当事件的来源为返回键,当当前显示页为主页按返回则退出,反之则显示第一页
    if (event.key === Qt.Key_Back) {
      //当前处于顶端的不是第一个
      //stackView的depth属性是只读的
      if (stackView.depth > 1) {
        //stackView的pop事件,详情看API,参数为空表示去第一页
        stackView.pop();
        //可继续操作
        event.accepted = true;
      } else { Qt.quit(); }//退出
    }
  }
  //StackView提供了一个基于堆栈的导航模型
  StackView {
    id: stackView //id必须唯一哦
    anchors.fill: parent //填充父组件的大小
    //设置初始化StackView界面
    initialItem: Item {
      //宽高都继承自父组件
      width: parent.width
      height: parent.height
      //内容为列表
      ListView {
        model: pageModel//使用id为pageModel的model
        anchors.fill: parent//充满父组件
        //实例化列表,为列表填充内容
        delegate: AndroidDelegate {
          text: title//内容标题
          //对应的内容页
          onClicked: stackView.push(Qt.resolvedUrl(page))
        }
      }
    }
  }
  //列表的model
  ListModel {
    id: pageModel
    //列表元素
    ListElement {
      title: "按钮"
      page: "content/ButtonPage.qml"
    }
    ListElement {
      title: "滑动条"
      page: "content/SliderPage.qml"
    }
    ListElement {
      title: "进度条"
      page: "content/ProgressBarPage.qml"
    }
    ListElement {
      title: "导航条"
      page: "content/TabBarPage.qml"
    }
    ListElement {
      title: "输入框"
      page: "content/TextInputPage.qml"
    }
    ListElement {
      title: "列表"
      page: "content/ListPage.qml"
    }
  }
  //接着继续stackView未完的代码
  //导航
  toolBar: BorderImage {//还记得么,前面我们讲过,当对象只有一个对象的时候可以这么写
    border.bottom: 8//底部边框
    source: "images/toolbar.png"//图片
    width: parent.width//宽继承自父组件的宽
    height: 60//高
    //返回按钮标示
    Rectangle {
      id: backButton//id
      width: opacity ? 50 : 0//不透明的时候为60,透明为0
      anchors.left: parent.left//在父组件的左边描点
      anchors.leftMargin: 20//左边距
      opacity: stackView.depth > 1 ? 1 : 0//界面不为主页的时候显示,为主页则不显示
      anchors.verticalCenter: parent.verticalCenter//相对于父组件垂直居中
      antialiasing: true
      height: 45//高
      radius: 4//边角
      color: backmouse.pressed ? "#222" : "transparent"//点击的时候显示色
      Behavior on opacity { NumberAnimation{} }//透明度的变化
      Image {
        anchors.verticalCenter: parent.verticalCenter//图片垂直居中
        source: "images/navigation_previous_item.png"//左箭头图片
      }
      MouseArea {
        id: backmouse
        anchors.fill: parent//相对于矩形图片区域描点
        anchors.margins: -10//边距
        onClicked: stackView.pop()//stackView返回操作,stackView的第一页
      }
    }

    Text {
      font.pixelSize: 25//字体大小
      Behavior on x { NumberAnimation{ easing.type: Easing.OutCubic} }//x轴的变化
      x: backButton.x + backButton.width + 20//在x轴的位置
      anchors.verticalCenter: parent.verticalCenter//垂直居中
      color: "white"//颜色
      text: "示例"//文字
    }
  }
}
完成之后,额,发现有一个地方报错了,就是关键字AndroidDelegate下面有红色报错提示,那表示代码正常,只是你需要加入一个名叫AndroidDelegate文件

下面去创建这个文件

单击QtCreator 导航的文件- 新建文件或项目-文件和类-选Qt - QML File (Qt Quick 2)-选择(图略)

输入文件名-AndroidDelegate.qml 路径注意是content文件夹里面(点浏览-选content)下一步

注意,要添加到你的项目中去


然后完成
输入下面代码,
//AndroidDelegate.qml
import QtQuick 2.0

Item {
  id: root
  width: parent.width //继承父组建的宽度
  height: 60

  property alias text: textitem.text //变量
  signal clicked
  //透明矩形,点击的时候可见
  Rectangle {
    anchors.fill: parent//描点充满父组建
    color: "#11ffffff"//透明
    visible: mouse.pressed//点击可见
  }
  //文本
  Text {
    id: textitem
    color: "white"//字体颜色
    font.pixelSize: 24//字体大小
    text: modelData//显示的文字
    anchors.verticalCenter: parent.verticalCenter//父组件垂直居中
    anchors.left: parent.left//靠左
    anchors.leftMargin: 30//左边距
  }
  //黑色矩形
  Rectangle {
    anchors.left: parent.left//描点位置为父组建左侧
    anchors.right: parent.right//右侧
    anchors.margins: 15//外边距
    height: 1//高
    color: "#424246"//黑色
  }
  //图片
  Image {
    anchors.right: parent.right//父组建描点
    anchors.rightMargin: 20//右边距20
    anchors.verticalCenter: parent.verticalCenter//垂直居中
    source: "../images/navigation_next_item.png"//图片路径
  }
  //点击响应
  MouseArea {
    id: mouse
    anchors.fill: parent//描点充满整个父组建
    onClicked: root.clicked()//父组建点击响应

  }
}
完成之后点击运行 

如果要看android版

请看QtCreator右下角的

 

点击电脑标志,选择就行了 


稍等片刻,点击运行,就可以看到手机效果了(前提是你的配置没问题) 


点击 


转场


因为其他界面文件,我们还没写,所以这里显示的就这样了

如需添加,请参照下面代码,因为时间关系,本人只写了其中的一个文件,其他的大家可以参考官方的源码(这个是根据源码改的,官方的项目叫touch)

//ButtonPage.qml
/**
  * 列表的按钮进入的界面
  * 后面我会在这个的基础上改一些东西
  */
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
//
Item {
  width: parent.width//继承父组建的宽
  height: parent.height//继承父组建的高

  property real progress: 0//自定义一个属性
  //动画效果:SequentialAnimation 作用是序列化多个动画,直白的说就是让多个动画依次发生
  SequentialAnimation on progress {
    loops: Animation.Infinite //loops:	   指定循环次数,Animation.Infinite表示无限循环
    running: true//运行状态
    NumberAnimation {
      from: 0
      to: 1//从0到1
      duration: 3000//持续时间3秒
    }
    NumberAnimation {
      from: 1
      to: 0
      duration: 3000
    }
  }

  Column {
    spacing: 40
    anchors.centerIn: parent//父元素的中心

    Button {
      text: "点击我,我不动"
      style: touchStyle
    }

    Button {
      style: touchStyle
      text: "点击我,我也不动"
    }

    Button {
      anchors.margins: 20
      style: touchStyle
      text: "点击我会返回哦"
      onClicked: if (stackView) stackView.pop()
    }
    //布局
    Row {
      spacing: 20
      Switch {
        style: switchStyle
      }
      Switch {
        style: switchStyle
      }
    }

  }
  //组件
  Component {
    id: touchStyle
    ButtonStyle {
      panel: Item {
        implicitHeight: 50
        implicitWidth: 320
        BorderImage {
          anchors.fill: parent
          antialiasing: true
          border.bottom: 8
          border.top: 8
          border.left: 8
          border.right: 8
          anchors.margins: control.pressed ? -4 : 0
          source: control.pressed ? "../images/button_pressed.png" : "../images/button_default.png"
          Text {
            text: control.text
            anchors.centerIn: parent
            color: "white"
            font.pixelSize: 23
            renderType: Text.NativeRendering
          }
        }
      }
    }
  }

  Component {
    id: switchStyle
    SwitchStyle {

      groove: Rectangle {
        implicitHeight: 50
        implicitWidth: 152
        Rectangle {
          anchors.top: parent.top
          anchors.left: parent.left
          anchors.bottom: parent.bottom
          width: parent.width/2 - 2
          height: 20
          anchors.margins: 2
          color: control.checked ? "#468bb7" : "#222"
          Behavior on color {ColorAnimation {}}
          Text {
            font.pixelSize: 23
            color: "white"
            anchors.centerIn: parent
            text: "开"
          }
        }
        Item {
          width: parent.width/2
          height: parent.height
          anchors.right: parent.right
          Text {
            font.pixelSize: 23
            color: "white"
            anchors.centerIn: parent
            text: "关"
          }
        }
        color: "#222"
        border.color: "#444"
        border.width: 2
      }
      handle: Rectangle {
        width: parent.parent.width/2
        height: control.height
        color: "#444"
        border.color: "#555"
        border.width: 2
      }
    }
  }
}



from: http://www.tuicool.com/articles/AFFzey

为了使更多的Qt者能尽快入门Qt,也为了QtQt Creator的快速普及,我们花费大量精力写出了这一系列教程。虽然教程的知识可能很浅显,虽然教程的语言可能不规范,但是它却被数十万网友所认可。我们会将这一系列教程一直写下去,它将涉及Qt的方方面面 一、Qt Creator的安装和hello world程序的编写 二、Qt Creator编写多窗口程序 三、Qt Creator登录对话框 四、Qt Creator添加菜单图标 五、Qt Creator布局管理器的使用 六、Qt Creator实现文本编辑 七、Qt Creator实现文本查找 八、Qt Creator实现状态栏显示 九、Qt Creator中鼠标键盘事件的处理实现自定义鼠标指针 十、Qt Creator中实现定时器和产生随机数 十一、Qt 2D绘图(一)绘制简单图形 十二、Qt 2D绘图(二)渐变填充 十三、Qt 2D绘图(三)绘制文字 十四、Qt 2D绘图(四)绘制路径 十五、Qt 2D绘图(五)显示图片 十六、Qt 2D绘图(六)坐标系统 十七、Qt 2D绘图(七)Qt坐标系统深入 十八、Qt 2D绘图(八)涂鸦板 十九、Qt 2D绘图(九)双缓冲绘图简介 二十、Qt 2D绘图(十)图形视图框架简介 二十一、Qt数据库(一)简介 二十二、Qt数据库(二)添加MySQL数据库驱动插件 二十三、Qt数据库(三)利用QSqlQuery类执行SQL语句(一) 二十四、Qt数据库(四)利用QSqlQuery类执行SQL语句(二) 二十五、Qt数据库(五)QSqlQueryModel 二十六、Qt数据库(六)QSqlTableModel 二十七、Qt数据库(七)QSqlRelationalTableModel 二十八、Qt数据库(八)XML(一) 二十九、Qt数据库(九)XML(二) 三十、Qt数据库(十)XML(三) 三十一、Qt 4.7.0及Qt Creator 2.0 beta版安装全程图解 三十二、第一个Qt Quick程序(QML程序) 三十三、体验QML演示程序 三十四、Qt Quick Designer介绍 三十五、QML组件 三十六、QML项目之Image和BorderImage 三十七、Flipable、Flickable和状态与动画 三十八、QML视图 三十九、QtDeclarative模块 四十、使用Nokia Qt SDK开发Symbian和Maemo终端软件 四十一、Qt网络(一)简介 四十二、Qt网络(二)HTTP编程 四十三、Qt网络(三)FTP(一) 四十四、Qt网络(四)FTP(二) 四十五、Qt网络(五)获取本机网络信息 四十六、Qt网络(六)UDP 四十七、Qt网络(七)TCP(一) 四十八、Qt网络(八)TCP(二)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值