用QT实现一个多页面系统参数管理窗口

有时候系统参数需要分类:比如一般参数,网络配置,高级配置,

这时需要分页进行配置与显示,用TabWidget可以解决这个问题。

源码类:

1)头文件

cpp 复制代码
#ifndef CONFIGWIDGET_H
#define CONFIGWIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QMessageBox>
#include <QTabWidget>

#include "ConfigManager.h"

class ConfigWidget : public QWidget {
    Q_OBJECT

public:
    ConfigWidget(ConfigManager *configManager, QWidget *parent = nullptr);
    ConfigWidget(QWidget *parent = nullptr);

private:
    void setupTabs();
    void loadSettings();
    void saveSettings();

    QTabWidget *tabWidget;
    //general Page
    QLineEdit *generalSetting1;
    QLineEdit *generalSetting2;

    //network page
    QLineEdit *networkIP;
    QLineEdit *networkPort;

    //advance page
    QLineEdit *advancedOption1;
    QLineEdit *advancedOption2;

    QSettings settings;
};


#endif // CONFIGWIDGET_H

2)实现文件

cpp 复制代码
#include "configwidget.h"

ConfigWidget::ConfigWidget(ConfigManager *configManager, QWidget *parent)
    : QWidget(parent)
    ,tabWidget(new QTabWidget(this))
{
    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->addWidget(tabWidget);

    setupTabs();
    loadSettings();
}

ConfigWidget::ConfigWidget(QWidget *parent)
    : QWidget(parent)
    ,tabWidget(new QTabWidget(this))
{
    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->addWidget(tabWidget);

    setupTabs();
    loadSettings();
}

void ConfigWidget::setupTabs()
{
    // General Configuration Page
    QWidget *generalPage = new QWidget(this);
    QFormLayout *generalLayout = new QFormLayout(generalPage);
    generalLayout->addRow(new QLabel("Setting 1:"), generalSetting1 = new QLineEdit(generalPage));
    generalLayout->addRow(new QLabel("Setting 2:"), generalSetting2 = new QLineEdit(generalPage));
    generalPage->setLayout(generalLayout);

    // Network Configuration Page
    QWidget *networkPage = new QWidget(this);
    QFormLayout *networkLayout = new QFormLayout(networkPage);
    networkLayout->addRow(new QLabel("IP Address:"), networkIP = new QLineEdit(networkPage));
    networkLayout->addRow(new QLabel("Port:"), networkPort = new QLineEdit(networkPage));
    networkPage->setLayout(networkLayout);

    // Advanced Configuration Page
    QWidget *advancedPage = new QWidget(this);
    QFormLayout *advancedLayout = new QFormLayout(advancedPage);
    advancedLayout->addRow(new QLabel("Option 1:"), advancedOption1 = new QLineEdit(advancedPage));
    advancedLayout->addRow(new QLabel("Option 2:"), advancedOption2 = new QLineEdit(advancedPage));
    advancedPage->setLayout(advancedLayout);

    // Adding pages to the QTabWidget
    tabWidget->addTab(generalPage, "General");
    tabWidget->addTab(networkPage, "Network");
    tabWidget->addTab(advancedPage, "Advanced");

    // Save Button
    QPushButton *saveButton = new QPushButton("Save", this);
    connect(saveButton, &QPushButton::clicked, this, &ConfigWidget::saveSettings);
    generalLayout->addWidget(saveButton);
}

void ConfigWidget::loadSettings()
{
    generalSetting1->setText(settings.value("general/setting1", "").toString());
    generalSetting2->setText(settings.value("general/setting2", "").toString());
    networkIP->setText(settings.value("network/ip", "").toString());
    networkPort->setText(settings.value("network/port", "").toString());
    advancedOption1->setText(settings.value("advanced/option1", "").toString());
    advancedOption2->setText(settings.value("advanced/option2", "").toString());
}

void ConfigWidget::saveSettings()
{
    settings.setValue("general/setting1", generalSetting1->text());
    settings.setValue("general/setting2", generalSetting2->text());
    settings.setValue("network/ip", networkIP->text());
    settings.setValue("network/port", networkPort->text());
    settings.setValue("advanced/option1", advancedOption1->text());
    settings.setValue("advanced/option2", advancedOption2->text());
}
相关推荐
深蓝海拓9 分钟前
Pyside6(PyQT5)中的QTableView与QSqlQueryModel、QSqlTableModel的联合使用
数据库·python·qt·pyqt
百流29 分钟前
scala文件编译相关理解
开发语言·学习·scala
Evand J2 小时前
matlab绘图——彩色螺旋图
开发语言·matlab·信息可视化
深度混淆2 小时前
C#,入门教程(04)——Visual Studio 2022 数据编程实例:随机数与组合
开发语言·c#
雁于飞3 小时前
c语言贪吃蛇(极简版,基本能玩)
c语言·开发语言·笔记·学习·其他·课程设计·大作业
wenxin-3 小时前
NS3网络模拟器中如何利用Gnuplot工具像MATLAB一样绘制各类图形?
开发语言·matlab·画图·ns3·lr-wpan
数据小爬虫@5 小时前
深入解析:使用 Python 爬虫获取苏宁商品详情
开发语言·爬虫·python
健胃消食片片片片5 小时前
Python爬虫技术:高效数据收集与深度挖掘
开发语言·爬虫·python
王老师青少年编程6 小时前
gesp(C++五级)(14)洛谷:B4071:[GESP202412 五级] 武器强化
开发语言·c++·算法·gesp·csp·信奥赛