用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());
}
相关推荐
Ajiang282473530425 分钟前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
幽兰的天空30 分钟前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024066 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it6 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神7 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海7 小时前
scala String
大数据·开发语言·scala