用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());
}
相关推荐
何曾参静谧几秒前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
q5673152327 分钟前
在 Bash 中获取 Python 模块变量列
开发语言·python·bash
许野平1 小时前
Rust: 利用 chrono 库实现日期和字符串互相转换
开发语言·后端·rust·字符串·转换·日期·chrono
也无晴也无风雨1 小时前
在JS中, 0 == [0] 吗
开发语言·javascript
狂奔solar1 小时前
yelp数据集上识别潜在的热门商家
开发语言·python
blammmp2 小时前
Java:数据结构-枚举
java·开发语言·数据结构
何曾参静谧2 小时前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
暗黑起源喵2 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong2 小时前
Java反射
java·开发语言·反射
Troc_wangpeng2 小时前
R language 关于二维平面直角坐标系的制作
开发语言·机器学习