话不多说,先一睹芳颜!
如果你也想达到这种效果,那咱们就开始吧!
目录
- 一.登录界面设计
- 1.ui登录设计
- 二.加载界面
- 1.lineEdit的密码输入模式
- 2.lineEdit按回车跳转的信号
- 3.密码的判断
- 4.创建加载界面
- 5.创建定时器来进行进度条的移动
- 6.定时器执行的槽函数
- 三.主机信息界面
- 1.主机信息系统文字
- 2.初始化按钮和布局
- 3.对每个按钮进行手动关联
- 四.实现相应槽函数功能
- 1.主机名
- 2.IPV4地址
- 3.IPV6地址
- 4.域名地址
- 5.网卡地址
- 6.网卡信息
- 五.完整代码
- 六.结语
一.登录界面设计
1.ui登录设计
可对Label标签进行居中显示.
也可以添加样式表,show出你的美.
label标签添加自定义大小的图片
二.加载界面
1.lineEdit的密码输入模式
输入的就是黑点,看不到真正的密码.
2.lineEdit按回车跳转的信号
3.密码的判断
如果密码错了,就继续输入,有三次机会,如果对了,就删除当前的界面.
4.创建加载界面
5.创建定时器来进行进度条的移动
当间隔时间一到,就会发出timeout信号,对槽函数**moveProgressBar()**进行执行!
6.定时器执行的槽函数
如果进度条的值加载到了一百,我们就跳转到主机信息的界面!
注意一定要将前面的布局删除,后面才能加新的布局.
因为我们在同一个窗口进行,所有后面的界面都是用代码来实现的.
要想显示就必须要加布局.
三.主机信息界面
1.主机信息系统文字
设置了渐变色,字体,居中,最小高度.
2.初始化按钮和布局
3.对每个按钮进行手动关联
四.实现相应槽函数功能
1.主机名
2.IPV4地址
3.IPV6地址
4.域名地址
5.网卡地址
6.网卡信息
五.完整代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QProgressBar>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QHostInfo>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_lineEditPwd_returnPressed();
void onButtonHostName();
void onButtonHostIPV4();
void onButtonHostIPV6();
void onButtonNameIP();
void onButtonNetworkInterfaceIP();
void onButtonNetworkInterfaceInfo();
void nameIP(const QHostInfo &host);
void moveProgressBar();
private:
Ui::Widget *ui;
QProgressBar*progressBar;
QLabel*loginLabel;
QTimer*timer;
QVBoxLayout *layout;
QPushButton* buttonHostName;
QPushButton* buttonHostIPV4;
QPushButton* buttonHostIPV6;
QPushButton* buttonNameIP;//域名
QPushButton* buttonNetworkInterfaceIP;//网卡
QPushButton* buttonNetworkInterfaceInfo;
QLabel*lableName;
QLineEdit*lineEdit;
void initBackground();
void initProgressBar();
void initTimer();
void initMenu();
void initSignalSlots();
};
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QStackedWidget>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QLayout>
#include <QDebug>
#include <QHostInfo>
#include <QNetworkInterface>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEditPwd->setEchoMode(QLineEdit::Password);
initBackground();
initTimer();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initBackground()
{
this->setFixedWidth(566);
this->setFixedHeight(366);
QPixmap pixmap(":/images/tx.png");
pixmap = pixmap.scaled(ui->labelImage->width(),ui->labelImage->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); // 保持宽高比
ui->labelImage->setPixmap(pixmap);
}
void Widget::initProgressBar()
{
progressBar=new QProgressBar;
progressBar->setValue(0);
progressBar->setRange(0,100);
loginLabel=new QLabel("正在登录中...");
loginLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
QFont font;
font.setBold(true);
font.setFamily("微软雅黑");
font.setPointSize(30);
loginLabel->setFont(font);
layout = new QVBoxLayout;
layout->addWidget(loginLabel);
layout->addWidget(progressBar);
// 将布局设置为窗口的布局
setLayout(layout);
}
void Widget::initTimer()
{
timer=new QTimer;
timer->setTimerType(Qt::CoarseTimer);
timer->setInterval(50);
connect(timer,SIGNAL(timeout()),this,SLOT(moveProgressBar()));
}
void Widget::initMenu()
{
lableName=new QLabel("主机信息系统");
lableName->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
QFont font;
font.setBold(true);
font.setFamily("微软雅黑");
font.setPointSize(30);
lableName->setFont(font);
lableName->setMinimumHeight(100);
QLinearGradient gradient(0, 0, lableName->width(), lableName->height()); // 创建线性渐变,从左上角到右下角
gradient.setColorAt(0, Qt::blue); // 设置渐变起始颜色
gradient.setColorAt(1, Qt::green); // 设置渐变结束颜色
QPalette palette;
palette.setBrush(QPalette::WindowText, QBrush(gradient)); // 将渐变设置为标签文字的画刷
lableName->setPalette(palette); // 设置标签的调色板
buttonHostName=new QPushButton("查看主机名");
buttonHostIPV4=new QPushButton("查看IPV4地址");
buttonHostIPV6=new QPushButton("查看IPV6地址");
lineEdit=new QLineEdit;
lineEdit->setPlaceholderText("请输入域名");
buttonNameIP=new QPushButton("查看域名IP地址");
buttonNetworkInterfaceIP=new QPushButton("查看网卡IP");
buttonNetworkInterfaceInfo =new QPushButton("查看网卡信息");
QVBoxLayout *Vlayout=new QVBoxLayout;
Vlayout->addWidget(buttonHostName);
Vlayout->addWidget(buttonHostIPV4);
Vlayout->addWidget(buttonHostIPV6);
QVBoxLayout *Vlayout2=new QVBoxLayout;
Vlayout2->addWidget(lineEdit);
Vlayout2->addWidget(buttonNameIP);
Vlayout2->addWidget(buttonNetworkInterfaceIP);
Vlayout2->addWidget(buttonNetworkInterfaceInfo);
QHBoxLayout *Hlayout=new QHBoxLayout;
Hlayout->addLayout(Vlayout);
Hlayout->addStretch();
Hlayout->addLayout(Vlayout2);
QVBoxLayout *Vlayout3=new QVBoxLayout;
Vlayout3->addWidget(lableName);
Vlayout3->addLayout(Hlayout);
setLayout(Vlayout3);
initSignalSlots();
}
void Widget::initSignalSlots()
{
connect(buttonHostName,SIGNAL(clicked()),this,SLOT(onButtonHostName()));
connect(buttonHostIPV4,SIGNAL(clicked()),this,SLOT(onButtonHostIPV4()));
connect(buttonHostIPV6,SIGNAL(clicked()),this,SLOT(onButtonHostIPV6()));
connect(buttonNameIP,SIGNAL(clicked()),this,SLOT(onButtonNameIP()));
connect(buttonNetworkInterfaceIP,SIGNAL(clicked()),this,SLOT(onButtonNetworkInterfaceIP()));
connect(buttonNetworkInterfaceInfo,SIGNAL(clicked()),this,SLOT(onButtonNetworkInterfaceInfo()));
}
void Widget::on_lineEditPwd_returnPressed()
{
static int count=0;
if(ui->lineEditPwd->text().trimmed()=="123456")
{
initProgressBar();
timer->start();
delete ui->groupBox;
}
else
{
if(++count>3)
{
QMessageBox::warning(this,"警告","密码输入错误过多,直接退出程序!");
this->close();
}
else
{
QMessageBox::information(this,"消息","密码输入错误,请重新输入!");
}
}
}
void Widget::onButtonHostName()
{
QString hostName=QHostInfo::localHostName();
QMessageBox::information(this,"主机信息","您的主机名为:"+hostName);
}
void Widget::onButtonHostIPV4()
{
QHostInfo info=QHostInfo::fromName(QHostInfo::localHostName());
QList<QHostAddress> list=info.addresses();//返回的不是指针
if(!list.isEmpty())
{
QString str;
for(int i=0;i<list.count();i++)
{
QHostAddress address=list[i];
if(address.protocol()==QAbstractSocket::IPv4Protocol)
{
str+=address.toString()+"\n";
}
}
QMessageBox::information(this,"主机信息","IPV4:\n"+str);
}
}
void Widget::onButtonHostIPV6()
{
QHostInfo info=QHostInfo::fromName(QHostInfo::localHostName());
QList<QHostAddress> list=info.addresses();//返回的不是指针
if(!list.isEmpty())
{
QString str;
for(int i=0;i<list.count();i++)
{
QHostAddress address=list[i];
if(address.protocol()==QAbstractSocket::IPv6Protocol)
{
str+=address.toString()+"\n";
}
}
QMessageBox::information(this,"主机信息","IPV6:\n"+str);
}
}
void Widget::onButtonNameIP()
{
QString name=lineEdit->text().trimmed();
if(name=="") return;
QHostInfo::lookupHost(name,this,SLOT(nameIP(QHostInfo)));
}
void Widget::onButtonNetworkInterfaceIP()
{
QList<QHostAddress> list=QNetworkInterface::allAddresses();
if(!list.isEmpty())
{
QString str;
for(int i=0;i<list.count();i++)
{
QHostAddress address=list[i];
if(address.protocol()==QAbstractSocket::IPv4Protocol)
{
str+=address.toString()+"\n";
}
}
QMessageBox::information(this,"网卡信息","网卡的IPV4:\n"+str);
}
}
void Widget::onButtonNetworkInterfaceInfo()
{
QList<QNetworkInterface> listInterface=QNetworkInterface::allInterfaces();
if(!listInterface.isEmpty())
{
QString str;
for(int i=0;i<listInterface.count();i++)
{
QNetworkInterface interface=listInterface[i];
str+="设备名称:"+interface.humanReadableName()+"\n";
str+="硬件地址:"+interface.hardwareAddress()+"\n";
QList<QNetworkAddressEntry> list=interface.addressEntries();
if(!list.isEmpty())
{
for(int i=0;i<list.count();i++)
{
QNetworkAddressEntry address=list[i];
str+="子网掩码:"+address.netmask().toString()+"\n";
str+="广播地址:"+address.broadcast().toString()+"\n";
str+="IP地址:"+address.ip().toString()+"\n\n";
}
}
}
QMessageBox::information(this,"网卡信息",str);
}
}
void Widget::nameIP(const QHostInfo &host)
{
QList<QHostAddress> list=host.addresses();//返回的不是指针
if(!list.isEmpty())
{
QString str;
for(int i=0;i<list.count();i++)
{
QHostAddress address=list[i];
if(address.protocol()==QAbstractSocket::IPv6Protocol)
{
str+=address.toString()+"\n";
}
}
QString name=lineEdit->text().trimmed();
QMessageBox::information(this,"域名信息",name+"的IPV6:\n"+str);
}
}
void Widget::moveProgressBar()
{
static int value=0;
progressBar->setValue(++value);
if(value==100)
{
timer->stop();
QMessageBox::StandardButton ret=
QMessageBox::information(this,"提示","登录成功!",QMessageBox::Ok,QMessageBox::NoButton);
if(ret==QMessageBox::Ok)
{
progressBar->hide();
loginLabel->hide();
delete layout;
initMenu();
}
}
}
六.结语
少即是多,慢即是快