【qt】获取主机信息系统

话不多说,先一睹芳颜!

在这里插入图片描述

如果你也想达到这种效果,那咱们就开始吧!

一.登录界面设计

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();
        }
    }
}

六.结语

少即是多,慢即是快

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值