I. Introduction to the environment
Qt: 5.12.6
Operating system: Win10 (64-bit)
Ii. Introduction of software effects and functions
Function: Domain name resolution (domain name to IP address)
Core code
3.1 the widget. The CPP
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
/* Engineering: SmartHome Date: 2021-04-26 author: DS Small Long ge environment: Win10 QT5.12.6 MinGW32 function: log display */
void Widget::Log_Text_Display(QString text)
{
QPlainTextEdit *plainTextEdit_log=ui->plainTextEdit_log;
// Set the cursor to the end of the text
plainTextEdit_log->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
// When the number of texts exceeds a certain range, it is cleared
if(plainTextEdit_log->toPlainText().size(a) >1024*4)
{
plainTextEdit_log->clear(a); } plainTextEdit_log->insertPlainText(text);
// Move the scroll bar to the bottom
QScrollBar *scrollbar = plainTextEdit_log->verticalScrollBar(a);if(scrollbar)
{
scrollbar->setSliderPosition(scrollbar->maximum()); }}// When the domain name is successfully resolved, the lookedUp slot function is called
void Widget::lookedUp(const QHostInfo &host)
{
if (host.error() != QHostInfo::NoError) {
Log_Text_Display(host.errorString() +"\n");
return;
}
foreach (QHostAddress address, host.addresses())
{
Log_Text_Display(address.toString() +"\n"); }}/* Project: IP_Addr_Analysis Date: 2021-06-01 Author: DS xiaolonggui environment: Win10 QT5.12.6 MinGW32 function: domain name parsing */
void Widget::on_pushButton_ip_get_clicked(a)
{
// Call nslookup to resolve the domain name, and then call QHostInfo to resolve some domain names.
// The reason for using QHostInfo is that it is too lazy to parse the string, so the QHostInfo signal reads the IP address directly.
QProcess process;
process.start(QString("nslookup %1").arg(ui->lineEdit_ip_name->text()));
process.waitForFinished(5000);
Log_Text_Display(process.readAll() +"\n");
QHostInfo::lookupHost(ui->lineEdit_ip_name->text(),this.SLOT(lookedUp(QHostInfo)));
}
/* Engineering: IP_Addr_Analysis Date: 2021-06-01 author: DS xiaolonggui environment: Win10 QT5.12.6 MinGW32 function: clear data */
void Widget::on_pushButton_clean_clicked(a)
{
ui->plainTextEdit_log->clear(a); }Copy the code
3.2 widget. H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QHostInfo>
#include <QScrollBar>
#include <QPlainTextEdit>
#include <QProcess>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget(a);private slots:
void lookedUp(const QHostInfo &host);
void Log_Text_Display(QString text);
void on_pushButton_ip_get_clicked(a);
void on_pushButton_clean_clicked(a);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
Copy the code