有时候系统参数需要分类:比如一般参数,网络配置,高级配置,
这时需要分页进行配置与显示,用TabWidget可以解决这个问题。
源码类:
1)头文件
#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)实现文件
#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());
}