QT系统授权及获取MAC、IP地址

 
 

界面

mainwindow.cc

#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "autherr.h"
#include "licenseimport.h"
#include <QNetworkInterface>
#include <QListWidget>
#include <iostream>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(onSendButtonClicked()));
}
QString MainWindow::getHostMacAddress()
{
    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface net, nets) {
        QListWidgetItem *item=new QListWidgetItem(net.hardwareAddress());
        ui->listWidget->addItem(item);
    }
    int nCnt = nets.count();
    QString strMacAddr = "";
    for(int i = 0;i < nCnt ;i++)
    {
        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
            strMacAddr = nets[i].hardwareAddress();
            break;
    }
    return strMacAddr;
}

void MainWindow::onSendButtonClicked()
{
    QString str=ui->lineEdit->text();
    int res= license_import(str.toLocal8Bit());
    switch (res) {
    case AUTH_INVALID_LICENSE:
        QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_INVALID_LICENSE",  QMessageBox::Yes);
        break;
    case AUTH_OPENFILE_ERROR:
        QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_OPENFILE_ERROR",  QMessageBox::Yes);
        break;
    case AUTH_LICENSE_TIMEOUT:
        QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_LICENSE_TIMEOUT",  QMessageBox::Yes);
        break;
    case AUTH_CONFIG_ERROR:
       QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_CONFIG_ERROR",  QMessageBox::Yes);
        break;
    case AUTH_NO_SUCH_PRODUCT:
        QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_NO_SUCH_PRODUCT",  QMessageBox::Yes);
        break;
    case AUTH_READ_DEVID_ERROR:
       QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_READ_DEVID_ERROR",  QMessageBox::Yes);
        break;
    case AUTH_ENCRYPT_ERROR:
        QMessageBox::information(NULL, "license tool", "Authorization failure, error code:AUTH_ENCRYPT_ERROR",  QMessageBox::Yes);
        break;
    default:
    break;
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}
main.cc
#include "mainwindow.hpp"
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.getHostMacAddress();
    return a.exec();
}
mianwindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public:
    QString getHostMacAddress();
private slots:
    void onSendButtonClicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HPP
mainwindow.ui

UI文件包括的控件有label,label_2,lineEdit,listWidget,pushButton。

涉及到的头文件autherr.h

#ifndef __h_autherr__
#define __h_autherr__
#define AUTH_INVALID_LICENSE   (-2)
#define AUTH_OPENFILE_ERROR    (-3)
#define AUTH_LICENSE_TIMEOUT   (-4)
#define AUTH_CONFIG_ERROR      (-5)
#define AUTH_NO_SUCH_PRODUCT   (-6)
#define AUTH_READ_DEVID_ERROR  (-7)
#define AUTH_ENCRYPT_ERROR     (-8)
#endif
liscenseimport.h
#ifndef __h_license_import__
#define __h_license_import__
#ifdef __cplusplus
extern "C"{
#endif
//const char* product_line_path = "/etc/fxconf/productline.xml";
//const char* license_data_path = "/etc/license.dat";
int license_import(const char* license_code);
#ifdef __cplusplus
}
#endif
#endif




猜你喜欢

转载自blog.csdn.net/qq_39573345/article/details/80681997