gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
使用qt开发一个点击按钮显示当前系统的计算机名和ip地址
- User
使用qt开发一个点击按钮显示当前系统的计算机名和ip地址
- Assistant
下面是一个简单的使用Qt开发的程序,可以点击按钮显示当前系统的计算机名和IP地址。
#include <QApplication> #include <QWidget> #include <QPushButton> #include <QLabel> #include <QHostInfo> class MainWindow : public QWidget { public: MainWindow() { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); QLabel* lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); lbl->setGeometry(QRect(50, 50, 200, 30)); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, [=]() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QString ipList; QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } // 更新标签文本 QString text = QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList); lbl->setText(text); }); // 设置窗口大小和标题 setFixedSize(300, 150); setWindowTitle("获取系统信息"); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }运行程序后,点击按钮即可显示当前系统的计算机名和IP地址。
- User
显示对应的main和mainwindow
- Assistant
下面是对应的main函数:
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }下面是对应的MainWindow类的实现:
#include "mainwindow.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); QLabel* lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); // 创建布局管理器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(btn); layout->addWidget(lbl); layout->setMargin(40); layout->setSpacing(20); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, [=]() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QString ipList; QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } // 更新标签文本 QString text = QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList); lbl->setText(text); }); setCentralWidget(new QWidget(this)); centralWidget()->setLayout(layout); setFixedSize(300, 150); setWindowTitle("获取系统信息"); } MainWindow::~MainWindow() { }注意,需要在头文件
mainwindow.h中添加类声明和成员变量声明,以及在构造函数中初始化布局管理器,并将其设置为主窗口的中央部件。具体请参考下面的完整代码:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QLabel; class QVBoxLayout; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QLabel* lbl; QVBoxLayout* layout; }; #endif // MAINWINDOW_H#include "mainwindow.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); // 创建布局管理器 layout = new QVBoxLayout(this); layout->addWidget(btn); layout->addWidget(lbl); layout->setMargin(40); layout->setSpacing(20); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, [=]() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QString ipList; QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } // 更新标签文本 QString text = QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList); lbl->setText(text); }); centralWidget()->setLayout(layout); setFixedSize(300, 150); setWindowTitle("获取系统信息"); } MainWindow::~MainWindow() { } - User
继续
- Assistant
完整的程序代码如下:
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QLabel; class QVBoxLayout; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QLabel* lbl; QVBoxLayout* layout; }; #endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); // 创建布局管理器 layout = new QVBoxLayout(this); layout->addWidget(btn); layout->addWidget(lbl); layout->setMargin(40); layout->setSpacing(20); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, [=]() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QString ipList; QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } // 更新标签文本 QString text = QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList); lbl->setText(text); }); centralWidget()->setLayout(layout); setFixedSize(300, 150); setWindowTitle("获取系统信息"); } MainWindow::~MainWindow() { }main.cpp
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } - User
继续
- Assistant
下面是一个稍微复杂一些的实现,使用了Qt的信号和槽机制,并且添加了一个自定义的对话框,用于显示计算机名和IP地址的详细信息。
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QLabel; class QVBoxLayout; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void showDetails(); private: QLabel* lbl; QVBoxLayout* layout; }; //============================================================================= class InfoDialog : public QDialog { Q_OBJECT public: InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent = nullptr); private: QLabel* lbl; }; #endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include "infodialog.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); // 创建布局管理器 layout = new QVBoxLayout(this); layout->addWidget(btn); layout->addWidget(lbl); layout->setMargin(40); layout->setSpacing(20); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &MainWindow::showDetails); centralWidget()->setLayout(layout); setFixedSize(300, 150); setWindowTitle("获取系统信息"); } MainWindow::~MainWindow() { } void MainWindow::showDetails() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); // 显示信息对话框 InfoDialog dlg(hostName, addressList, this); dlg.exec(); } //============================================================================= InfoDialog::InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget *parent) : QDialog(parent) { setWindowTitle("系统信息"); // 创建标签和按钮 QString ipList; for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } lbl = new QLabel(this); lbl->setText(QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList)); QPushButton* btn = new QPushButton("关闭", this); // 创建布局管理器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(lbl); layout->addWidget(btn); layout->setMargin(20); layout->setSpacing(10); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &InfoDialog::close); setLayout(layout); resize(300, 200); }infodialog.h
#ifndef INFODIALOG_H #define INFODIALOG_H #include <QDialog> class QLabel; class InfoDialog : public QDialog { Q_OBJECT public: InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent = nullptr); private: QLabel* lbl; }; #endif // INFODIALOG_Hinfodialog.cpp
#include "infodialog.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> InfoDialog::InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent) : QDialog(parent) { setWindowTitle("系统信息"); // 创建标签和按钮 QString ipList; for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } lbl = new QLabel(this); lbl->setText(QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList)); QPushButton* btn = new QPushButton("关闭", this); // 创建布局管理器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(lbl); layout->addWidget(btn); layout->setMargin(20); layout->setSpacing(10); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &InfoDialog::close); setLayout(layout); resize(300, 200); } - User
继续
- Assistant
下面是对应的 main 函数:
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }注意,在使用自定义对话框时,需要在头文件
infodialog.h中添加类声明和成员变量声明,以及在实现文件infodialog.cpp中实现构造函数。具体请参考下面的完整代码:mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QLabel; class QVBoxLayout; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void showDetails(); private: QLabel* lbl; QVBoxLayout* layout; }; //============================================================================= class InfoDialog : public QDialog { Q_OBJECT public: InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent = nullptr); private: QLabel* lbl; }; #endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include "infodialog.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建按钮和标签 QPushButton* btn = new QPushButton("获取系统信息", this); lbl = new QLabel("请点击按钮", this); lbl->setAlignment(Qt::AlignCenter); // 创建布局管理器 layout = new QVBoxLayout(this); layout->addWidget(btn); layout->addWidget(lbl); layout->setMargin(40); layout->setSpacing(20); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &MainWindow::showDetails); centralWidget()->setLayout(layout); setFixedSize(300, 150); setWindowTitle("获取系统信息"); } MainWindow::~MainWindow() { } void MainWindow::showDetails() { // 获取主机名和IP列表 QString hostName = QHostInfo::localHostName(); QList<QHostAddress> addressList = QHostInfo::fromName(hostName).addresses(); // 显示信息对话框 InfoDialog dlg(hostName, addressList, this); dlg.exec(); } //============================================================================= InfoDialog::InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget *parent) : QDialog(parent) { setWindowTitle("系统信息"); // 创建标签和按钮 QString ipList; for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } lbl = new QLabel(this); lbl->setText(QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList)); QPushButton* btn = new QPushButton("关闭", this); // 创建布局管理器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(lbl); layout->addWidget(btn); layout->setMargin(20); layout->setSpacing(10); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &InfoDialog::close); setLayout(layout); resize(300, 200); }infodialog.h
#ifndef INFODIALOG_H #define INFODIALOG_H #include <QDialog> class QLabel; class InfoDialog : public QDialog { Q_OBJECT public: InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent = nullptr); private: QLabel* lbl; }; #endif // INFODIALOG_Hinfodialog.cpp
#include "infodialog.h" #include <QPushButton> #include <QLabel> #include <QVBoxLayout> #include <QHostInfo> InfoDialog::InfoDialog(const QString& hostName, const QList<QHostAddress>& addressList, QWidget* parent) : QDialog(parent) { setWindowTitle("系统信息"); // 创建标签和按钮 QString ipList; for (int i = 0; i < addressList.size(); ++i) { QHostAddress address = addressList.at(i); if (!address.isNull() && address.protocol() == QAbstractSocket::IPv4Protocol) { ipList += address.toString() + "\n"; } } lbl = new QLabel(this); lbl->setText(QString("主机名:%1\nIP地址:\n%2").arg(hostName).arg(ipList)); QPushButton* btn = new QPushButton("关闭", this); // 创建布局管理器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(lbl); layout->addWidget(btn); layout->setMargin(20); layout->setSpacing(10); // 绑定按钮点击事件 connect(btn, &QPushButton::clicked, this, &InfoDialog::close); setLayout(layout); resize(300, 200); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.