当前位置: 首页 > news >正文

QT桌面项目(状态栏和导航栏设置)

文章目录

  • 前言
  • 一、状态栏
  • 二、导航栏
  • 三、同时添加状态栏和导航栏
  • 总结


前言

为了和我们这个项目做的更加真实,这里为我们的项目添加上状态栏和导航栏让他变成更加接近手机的桌面效果。

一、状态栏

这个状态栏就是显示时间和wifi状态,电池电量的,这里把颜色都设置为白色因为设置为白色后就不会受到壁纸更换的影响了。
在这里插入图片描述
那么如何来编写这个状态栏呢?这里先建一个MyStatusBar的类:
他继承QWidegt。

这个状态栏的编写方法也是非常简单的,只需要在这个QWidegt中使用QHBoxLayout布局管理器进行布局即可,使用addStretch函数在中间添加一个可伸缩的空间,让显示时间和wifi状态进行左右分隔。

MyStatusBar.cpp

#include "MyStatusBar.h"
#include <QHBoxLayout>
#include <QDateTime>
#include <QPixmap>MyStatusBar::MyStatusBar(QWidget *parent): QWidget{parent}, m_time(this), m_Electricity(this), m_Signal(this)
{QHBoxLayout* Hlayout = new QHBoxLayout(this);QDateTime currentTime = QDateTime::currentDateTime();m_time.setStyleSheet("color : white ;");m_time.setText(currentTime.toString("hh:mm"));QPixmap pix;pix = QPixmap(":/signal.png").scaled(30, 30);m_Signal.setPixmap(pix);pix = QPixmap(":/Power.png").scaled(30, 30);m_Electricity.setPixmap(pix);Hlayout->addWidget(&m_time);Hlayout->addStretch(); // 添加一个可伸缩的空间Hlayout->addWidget(&m_Signal);Hlayout->addWidget(&m_Electricity);Hlayout->setContentsMargins(5, 0, 5, 0);m_timer.start(30000);connect(&m_timer, SIGNAL(timeout()), this, SLOT(Timeout()));}void MyStatusBar::Timeout()
{QDateTime currentTime = QDateTime::currentDateTime();m_time.setText(currentTime.toString("hh:mm"));
}

MyStatusBar.h

#ifndef MYSTATUSBAR_H
#define MYSTATUSBAR_H#include <QWidget>
#include <QLabel>
#include <QTimer>class MyStatusBar : public QWidget
{Q_OBJECTQLabel m_time;QLabel m_Electricity;QLabel m_Signal;QTimer m_timer;public:explicit MyStatusBar(QWidget *parent = nullptr);signals:protected slots:void Timeout();};#endif // MYSTATUSBAR_H

使用方法:
使用垂直布局管理器将这个状态栏添加进入主界面即可。

 MyStatusBar* mystatusbar = new MyStatusBar();QVBoxLayout* Vlayout = new QVBoxLayout(this);Vlayout->addWidget(mystatusbar);Vlayout->addStretch();Vlayout->setSpacing(0);Vlayout->setContentsMargins(0, 0, 0, 0);

二、导航栏

在这里插入图片描述

导航栏就是像手机下面的三个小点,指示现在是在第几个界面。这几个小点使用QPushbutton来设置即可。同样的也将他设置为白色防止壁纸对他的干扰。

和上面的状态栏一样,我们这里也新建一个NavigationBar类:

NavigationBar.h

在NavigationBar这个类中提供三个按键(你的主界面有几个就提供几个按键)

再提供两个按键修改函数ButtonNormalStyle和ButtonSelectStyle函数,分别设置不同状态下的按键。

当显示到对应的界面时按键的样式变为长方形圆角样式,当没有显示到对应界面时按键变为圆形样式。

    QPushButton button1;QPushButton button2;QPushButton button3;void ButtonNormalStyle(QPushButton& button);void ButtonSelectStyle(QPushButton& button);

NavigationBar.cpp
使用水平布局管理器将这三个按键管理起来,并且在头部和尾部使用addStretch函数让这三个按键排布在中间位置。

QHBoxLayout* Hlayout = new QHBoxLayout();Hlayout->addStretch();
Hlayout->addWidget(&button1);
Hlayout->addWidget(&button2);
Hlayout->addWidget(&button3);
Hlayout->addStretch();void Widget::ButtonNormalStyle(QPushButton& button)
{button.setFixedSize(10, 10);button.setStyleSheet("QPushButton {\border: none;\border-radius: 5px;\background-color: rgba(255, 255, 255, 0.5);\color: white;\text-align: center;\}");
}void Widget::ButtonSelectStyle(QPushButton& button)
{button.setFixedSize(15, 10);button.setStyleSheet("QPushButton {\border: none;\border-radius: 5px;\background-color: white;\color: white;\text-align: center;\}");
}

三、同时添加状态栏和导航栏

同时将状态栏和导航栏添加进桌面也是很简单的,只需要使用QVBoxLayout垂直布局管理器进行管理即可,将主界面显示在中间位置即可完成效果。

总结

当我们完成这一步后我们的桌面就有模有样了哈哈哈。希望大家可以继续跟着我学习,一起做出一个完整的桌面项目。

http://www.lryc.cn/news/89600.html

相关文章:

  • 数据链路层:点对点协议PPP
  • C/C++读取txt文件中的float数据并用指针存储
  • 对KMP算法的一点碎碎念——上篇
  • 算法---边界着色
  • 二叉排序树的查找、插入、删除
  • 《Opencv3编程入门》学习笔记—第三章
  • 如何从Ubuntu Linux中删除Firefox Snap?
  • 数学建模的初阶-快速上手
  • 复习向 C/C++ 编程语言简介和概括(C++复习向p1)
  • DRF之过滤,排序,分页
  • 我的Redis学习,共写了14篇博客文章
  • mPython软件使用指南
  • 龙芯2K1000实战开发-系统配置详解
  • 【一起撸个DL框架】5 实现:自适应线性单元
  • 开箱即用的工具函数库xijs更新指南(v1.2.6)
  • 【Netty】ChannelPipeline源码分析(五)
  • 并行计算技术解密:MPI和OpenMP的学习和应用指南
  • 什么是自动化测试框架?我们该如何搭建自动化测试框架?
  • Debezium报错处理系列之六十七:TopicAuthorizationException: Not authorized to access topics
  • javaWebssh中小学课件资源系统myeclipse开发mysql数据库MVC模式java编程计算机网页设计
  • MySQL高级查询操作
  • Day53【动态规划】1143.最长公共子序列、1035.不相交的线、53.最大子序和
  • Three.js--》实现3d地球模型展示
  • <SQL>《SQL命令(含例句)精心整理版(6)》
  • 信息系统建设和服务能力评估证书CS
  • vue3引入路由
  • 前后端联调跨域问题
  • day11 - 手写数字笔迹细化
  • C++ QT QDBus基操
  • STM32的SPI外设