利用QT实现主界面APP应用开发之经典

      在嵌入式的开发过程中,利用好应用开发也是不错的选择。

     今天主要和大家聊一聊,如何利用Qt来开发APP主界面,在Qt中提供了QStackedWdget与QTableView这样的控件方便页面切换,但是这种方法比较生硬,体验感较差。也可以自己实现对应滑动界面的设计。

第一:滑动界面的设计基础

#include "slidepage.h"#include <QDebug>#include <QPropertyAnimation>SlidePage::SlidePage(QWidget *parent):QWidget(parent),pageIndex(0),pageCount(0),draggingFlag(false){ pageIndicator.clear(); this->setMinimumSize(400, 300); this->setAttribute(Qt::WA_TranslucentBackground, true); scrollArea = new QScrollArea(this); scrollArea->setAlignment(Qt::AlignCenter); mainWidget = new QWidget(); mainWidget->setStyleSheet("background: transparent"); scrollArea->setWidget(mainWidget); scrollArea->setStyleSheet("background: transparent"); bottomWidget = new QWidget(this); bottomWidget->setStyleSheet("background: transparent"); bottomHBoxLayout = new QHBoxLayout(); bottomWidget->setLayout(bottomHBoxLayout); bottomHBoxLayout->setContentsMargins(0, 0, 0, 0); bottomHBoxLayout->setAlignment(Qt::AlignCenter);/* 关闭滚动条显示 */ scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff); scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff); /* 滚屏对象 */ scroller = QScroller::scroller(scrollArea); QScroller::ScrollerGestureType gesture = QScroller::LeftMouseButtonGesture; scroller->grabGesture(scrollArea, gesture);/* 获取属性 */ QScrollerProperties properties = scroller->scrollerProperties();/* 设置滑动的时间,值越大,时间越短 */ properties.setScrollMetric(QScrollerProperties::SnapTime, 0.5);/* 设置滑动速度 */ properties.setScrollMetric(QScrollerProperties::MinimumVelocity, 1); scroller->setScrollerProperties(properties);/* 布局 */ hBoxLayout = new QHBoxLayout(); hBoxLayout->setContentsMargins(0, 0, 0, 0); hBoxLayout->setSpacing(0); mainWidget->setLayout(hBoxLayout);/* 定时器,用于判断用户是否是拖动屏幕,区分滑动,超过 300ms 表示拖动 */ timer = new QTimer(this); connect(scrollArea->horizontalScrollBar(),SIGNAL(valueChanged(int)), this, SLOT(hScrollBarValueChanged(int))); connect(scroller, SIGNAL(stateChanged(QScroller::State)), this,SLOT(onStateChanged(QScroller::State))); connect(timer, SIGNAL(timeout()), this, SLOT(onTimerTimeOut())); connect(this, SIGNAL(currentPageIndexChanged(int)), this,SLOT(onCurrentPageIndexChanged(int)));}

    上面的这些描述,只是一个滑动界面的类,如果需要控制的话,还需要往里面添加对应的内容。

第二:APP主界面实现方法

     在项目正常运行之后,可以看到一个APP主界面,这个界面不能点击,如果需要控制的话再通过链接到界面上的按钮点击信号,可以是点击按钮之后打开一个新界面,完成事件的交互。

项目文件夹下内容解释:

03_appmainview.pro 项目下:

1、appdemo 文件夹为车载音乐 APP 页面,只是界面,不带实际功能!

2、slidepage 文件夹为笔者原创的一个滑动页面类,在这个类里,我们可以使用 addPage()方法来添加页面,当添加的页面大于 2 页时,就可以滑动切换页面了。

3、Headers 文件夹为 03_appmainview.pro 的头文件。

4、Sources 文件夹为 03_appmainview.pro 的源文件。

5、Resource 文件夹为 03_appmainview.pro 的源文件。主要是存放一张背景图片。

实例参考链接:

https://download.csdn.net/download/weixin_41114301/85930012icon-default.png?t=M5H6https://download.csdn.net/download/weixin_41114301/85930012

第三:实现分析

    在运行的过程中,会有三个界面,可以利用利用手动的方式进行滑动,从运行效果上来说,还是不错的。

总结:在编写实现APP的过程中,需要对其中的API函数的非常熟悉,这个就要投入大量的精力在里面,学习各个函数的用法。