用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());
}
相关推荐
程序媛学姐6 分钟前
SpringKafka错误处理:重试机制与死信队列
java·开发语言·spring·kafka
2401_8401922714 分钟前
如何学习一门计算机技术
开发语言·git·python·devops
巷北夜未央28 分钟前
Python每日一题(14)
开发语言·python·算法
雾月551 小时前
LeetCode 914 卡牌分组
java·开发语言·算法·leetcode·职场和发展
Y.O.U..1 小时前
今日八股——C++
开发语言·c++·面试
weixin_307779131 小时前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
Xiaok10181 小时前
解决 Hugging Face SentenceTransformer 下载失败的完整指南:ProxyError、SSLError与手动下载方案
开发语言·神经网络·php
绿草在线1 小时前
Mock.js虚拟接口
开发语言·javascript·ecmascript
go_bai2 小时前
Linux环境基础开发工具——(2)vim
linux·开发语言·经验分享·笔记·vim·学习方法
小郝 小郝2 小时前
【C语言】strstr查找字符串函数
c语言·开发语言