用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());
}
相关推荐
engchina6 分钟前
如何在 Python 中忽略烦人的警告?
开发语言·人工智能·python
向宇it6 分钟前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
诚丞成31 分钟前
计算世界之安生:C++继承的文水和智慧(上)
开发语言·c++
Smile灬凉城66643 分钟前
反序列化为啥可以利用加号绕过php正则匹配
开发语言·php
lsx2024061 小时前
SQL MID()
开发语言
Dream_Snowar1 小时前
速通Python 第四节——函数
开发语言·python·算法
西猫雷婶1 小时前
python学opencv|读取图像(十四)BGR图像和HSV图像通道拆分
开发语言·python·opencv
鸿蒙自习室1 小时前
鸿蒙UI开发——组件滤镜效果
开发语言·前端·javascript
言、雲1 小时前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
汪洪墩2 小时前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium