⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settingsviewer.cpp

📁 Qt4的一些例子
💻 CPP
字号:
#include <QtGui>#include "settingsviewer.h"SettingsViewer::SettingsViewer(QWidget *parent)    : QDialog(parent){    organization = "Trolltech";    application = "Designer";    treeWidget = new QTreeWidget;    treeWidget->setColumnCount(2);    treeWidget->setHeaderLabels(            QStringList() << tr("Key") << tr("Value"));    treeWidget->header()->setResizeMode(0, QHeaderView::Stretch);    treeWidget->header()->setResizeMode(1, QHeaderView::Stretch);    openButton = new QPushButton(tr("&Open..."));    openButton->setDefault(true);    closeButton = new QPushButton(tr("Close"));    connect(openButton, SIGNAL(clicked()), this, SLOT(open()));    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));    QHBoxLayout *bottomLayout = new QHBoxLayout;    bottomLayout->addStretch();    bottomLayout->addWidget(openButton);    bottomLayout->addWidget(closeButton);    QVBoxLayout *mainLayout = new QVBoxLayout;    mainLayout->addWidget(treeWidget);    mainLayout->addLayout(bottomLayout);    setLayout(mainLayout);    setWindowTitle(tr("Settings Viewer"));    readSettings();}void SettingsViewer::open(){    QDialog dialog(this);    QLabel *orgLabel = new QLabel(tr("&Organization:"));    QLineEdit *orgLineEdit = new QLineEdit(organization);    orgLabel->setBuddy(orgLineEdit);    QLabel *appLabel = new QLabel(tr("&Application:"));    QLineEdit *appLineEdit = new QLineEdit(application);    appLabel->setBuddy(appLineEdit);    QPushButton *okButton = new QPushButton(tr("OK"));    okButton->setDefault(true);    QPushButton *cancelButton = new QPushButton(tr("Cancel"));    connect(okButton, SIGNAL(clicked()), &dialog, SLOT(accept()));    connect(cancelButton, SIGNAL(clicked()), &dialog, SLOT(reject()));    QHBoxLayout *buttonLayout = new QHBoxLayout;    buttonLayout->addStretch();    buttonLayout->addWidget(okButton);    buttonLayout->addWidget(cancelButton);    QGridLayout *gridLayout = new QGridLayout;    gridLayout->addWidget(orgLabel, 0, 0);    gridLayout->addWidget(orgLineEdit, 0, 1);    gridLayout->addWidget(appLabel, 1, 0);    gridLayout->addWidget(appLineEdit, 1, 1);    QVBoxLayout *mainLayout = new QVBoxLayout;    mainLayout->addLayout(gridLayout);    mainLayout->addLayout(buttonLayout);    dialog.setLayout(mainLayout);    dialog.setWindowTitle(tr("Choose Settings"));    if (dialog.exec()) {        organization = orgLineEdit->text();        application = appLineEdit->text();        readSettings();    }}void SettingsViewer::readSettings(){    QSettings settings(organization, application);    treeWidget->clear();    addChildSettings(settings, 0, "");    treeWidget->sortByColumn(0);    treeWidget->setFocus();    setWindowTitle(tr("Settings Viewer - %1 by %2")                   .arg(application).arg(organization));}void SettingsViewer::addChildSettings(QSettings &settings,        QTreeWidgetItem *parent, const QString &group){    QTreeWidgetItem *item;    settings.beginGroup(group);    foreach (QString key, settings.childKeys()) {        if (parent) {            item = new QTreeWidgetItem(parent);        } else {            item = new QTreeWidgetItem(treeWidget);        }        item->setText(0, key);        item->setText(1, settings.value(key).toString());    }    foreach (QString group, settings.childGroups()) {        if (parent) {            item = new QTreeWidgetItem(parent);        } else {            item = new QTreeWidgetItem(treeWidget);        }        item->setText(0, group);        addChildSettings(settings, item, group);    }    settings.endGroup();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -