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

简介: 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_OBJECT
    QLabel 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垂直布局管理器进行管理即可,将主界面显示在中间位置即可完成效果。


总结

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


相关文章
|
7月前
|
网络协议 容器
【qt】 TCP编程小项目
【qt】 TCP编程小项目
127 0
|
4月前
|
存储 文件存储 数据库
【QT项目】QT项目综合练习之简易计数器(QT6+文件存储)
【QT项目】QT项目综合练习之简易计数器(QT6+文件存储)
|
4月前
|
XML 数据可视化 C语言
001 Qt_从零开始创建项目
本文是Qt专栏的第一篇,介绍了如何创建一个Qt项目。
171 4
|
7月前
【qt】平面CAD(计算机辅助设计 )项目 上
【qt】平面CAD(计算机辅助设计 )项目 上
85 0
|
7月前
【Qt项目专栏】贪吃蛇小游戏1.0
【Qt项目专栏】贪吃蛇小游戏1.0
210 5
|
7月前
【qt】项目移植
【qt】项目移植
69 0
【qt】项目移植
|
7月前
|
API UED
【Qt 学习笔记】Qt窗口 | 状态栏 | QStatusBar的使用及说明
【Qt 学习笔记】Qt窗口 | 状态栏 | QStatusBar的使用及说明
898 4
|
7月前
CMake自动打包--Qt项目
CMake自动打包--Qt项目
100 0
|
8月前
QT设置widget背景图片
该内容介绍如何在Qt中为控件添加背景图片。主要方法包括:1) 在样式表中使用`border-image`属性指定控件及其背景图片;2) 使用调色板`QPalette`设置图片,但可能导致窗口显示不下;3) 在`paintEvent`中绘制图片,适合自定义绘图但不适用于子窗口;4) 通过覆盖一个大小与窗口相同的`QLabel`来设置背景图片,可实现动态背景。推荐使用样式表设置背景,简单高效且适合子窗口。
405 3
|
9月前
|
C++ Windows
第1个Qt项目:计算器
第1个Qt项目:计算器
第1个Qt项目:计算器