Settings Editor Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include <QtWidgets>
#include
"locationdialog.h"
LocationDialog::
LocationDialog(QWidget *
parent)
:
QDialog(parent)
{
formatComboBox =
new
QComboBox;
formatComboBox-&
gt;addItem(tr("Native"
));
formatComboBox-&
gt;addItem(tr("INI"
));
scopeComboBox =
new
QComboBox;
scopeComboBox-&
gt;addItem(tr("User"
));
scopeComboBox-&
gt;addItem(tr("System"
));
organizationComboBox =
new
QComboBox;
organizationComboBox-&
gt;addItem(tr("QtProject"
));
organizationComboBox-&
gt;setEditable(true
);
applicationComboBox =
new
QComboBox;
applicationComboBox-&
gt;addItem(tr("Any"
));
applicationComboBox-&
gt;addItem(tr("Qt Creator"
));
applicationComboBox-&
gt;addItem(tr("Application Example"
));
applicationComboBox-&
gt;addItem(tr("Assistant"
));
applicationComboBox-&
gt;addItem(tr("Designer"
));
applicationComboBox-&
gt;addItem(tr("Linguist"
));
applicationComboBox-&
gt;setEditable(true
);
applicationComboBox-&
gt;setCurrentIndex(1
);
formatLabel =
new
QLabel(tr("&Format:"
));
formatLabel-&
gt;setBuddy(formatComboBox);
scopeLabel =
new
QLabel(tr("&Scope:"
));
scopeLabel-&
gt;setBuddy(scopeComboBox);
organizationLabel =
new
QLabel(tr("&Organization:"
));
organizationLabel-&
gt;setBuddy(organizationComboBox);
applicationLabel =
new
QLabel(tr("&Application:"
));
applicationLabel-&
gt;setBuddy(applicationComboBox);
locationsGroupBox =
new
QGroupBox(tr("Setting Locations"
));
QStringList labels;
labels &
lt;&
lt; tr("Location"
) &
lt;&
lt; tr("Access"
);
locationsTable =
new
QTableWidget;
locationsTable-&
gt;setSelectionMode(QAbstractItemView::
SingleSelection);
locationsTable-&
gt;setSelectionBehavior(QAbstractItemView::
SelectRows);
locationsTable-&
gt;setEditTriggers(QAbstractItemView::
NoEditTriggers);
locationsTable-&
gt;setColumnCount(2
);
locationsTable-&
gt;setHorizontalHeaderLabels(labels);
locationsTable-&
gt;horizontalHeader()-&
gt;setSectionResizeMode(0
, QHeaderView::
Stretch);
locationsTable-&
gt;horizontalHeader()-&
gt;resizeSection(1
, 180
);
connect(locationsTable, &
amp;QTableWidget::
itemActivated, this
, &
amp;LocationDialog::
itemActivated);
buttonBox =
new
QDialogButtonBox(QDialogButtonBox::
Ok |
QDialogButtonBox::
Cancel);
connect(formatComboBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
activated),
this
, &
amp;LocationDialog::
updateLocationsTable);
connect(scopeComboBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
activated),
this
, &
amp;LocationDialog::
updateLocationsTable);
connect(organizationComboBox-&
gt;lineEdit(),
&
amp;QLineEdit::
editingFinished,
this
, &
amp;LocationDialog::
updateLocationsTable);
connect(applicationComboBox-&
gt;lineEdit(),
&
amp;QLineEdit::
editingFinished,
this
, &
amp;LocationDialog::
updateLocationsTable);
connect(applicationComboBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
activated),
this
, &
amp;LocationDialog::
updateLocationsTable);
connect(buttonBox, &
amp;QDialogButtonBox::
accepted, this
, &
amp;QDialog::
accept);
connect(buttonBox, &
amp;QDialogButtonBox::
rejected, this
, &
amp;QDialog::
reject);
QVBoxLayout *
locationsLayout =
new
QVBoxLayout(locationsGroupBox);
locationsLayout-&
gt;addWidget(locationsTable);
QGridLayout *
mainLayout =
new
QGridLayout(this
);
mainLayout-&
gt;addWidget(formatLabel, 0
, 0
);
mainLayout-&
gt;addWidget(formatComboBox, 0
, 1
);
mainLayout-&
gt;addWidget(scopeLabel, 1
, 0
);
mainLayout-&
gt;addWidget(scopeComboBox, 1
, 1
);
mainLayout-&
gt;addWidget(organizationLabel, 2
, 0
);
mainLayout-&
gt;addWidget(organizationComboBox, 2
, 1
);
mainLayout-&
gt;addWidget(applicationLabel, 3
, 0
);
mainLayout-&
gt;addWidget(applicationComboBox, 3
, 1
);
mainLayout-&
gt;addWidget(locationsGroupBox, 4
, 0
, 1
, 2
);
mainLayout-&
gt;addWidget(buttonBox, 5
, 0
, 1
, 2
);
updateLocationsTable();
setWindowTitle(tr("Open Application Settings"
));
resize(650
, 400
);
}
QSettings::
Format LocationDialog::
format() const
{
if
(formatComboBox-&
gt;currentIndex() ==
0
)
return
QSettings::
NativeFormat;
else
return
QSettings::
IniFormat;
}
QSettings::
Scope LocationDialog::
scope() const
{
if
(scopeComboBox-&
gt;currentIndex() ==
0
)
return
QSettings::
UserScope;
else
return
QSettings::
SystemScope;
}
QString LocationDialog::
organization() const
{
return
organizationComboBox-&
gt;currentText();
}
QString LocationDialog::
application() const
{
if
(applicationComboBox-&
gt;currentText() ==
tr("Any"
))
return
QString();
else
return
applicationComboBox-&
gt;currentText();
}
void
LocationDialog::
itemActivated(QTableWidgetItem *
)
{
buttonBox-&
gt;button(QDialogButtonBox::
Ok)-&
gt;animateClick();
}
void
LocationDialog::
updateLocationsTable()
{
locationsTable-&
gt;setUpdatesEnabled(false
);
locationsTable-&
gt;setRowCount(0
);
for
(int
i =
0
; i &
lt; 2
; ++
i) {
if
(i ==
0
&
amp;&
amp; scope() ==
QSettings::
SystemScope)
continue
;
QSettings::
Scope actualScope =
(i ==
0
) ? QSettings::
UserScope
:
QSettings::
SystemScope;
for
(int
j =
0
; j &
lt; 2
; ++
j) {
if
(j ==
0
&
amp;&
amp; application().isEmpty())
continue
;
QString actualApplication;
if
(j ==
0
)
actualApplication =
application();
QSettings settings(format(), actualScope, organization(),
actualApplication);
int
row =
locationsTable-&
gt;rowCount();
locationsTable-&
gt;setRowCount(row +
1
);
QTableWidgetItem *
item0 =
new
QTableWidgetItem(QDir::
toNativeSeparators(settings.fileName()));
QTableWidgetItem *
item1 =
new
QTableWidgetItem;
bool
disable =
(settings.childKeys().isEmpty()
&
amp;&
amp; settings.childGroups().isEmpty());
if
(row ==
0
) {
if
(settings.isWritable()) {
item1-&
gt;setText(tr("Read-write"
));
disable =
false
;
}
else
{
item1-&
gt;setText(tr("Read-only"
));
}
buttonBox-&
gt;button(QDialogButtonBox::
Ok)-&
gt;setDisabled(disable);
}
else
{
item1-&
gt;setText(tr("Read-only fallback"
));
}
if
(disable) {
item0-&
gt;setFlags(item0-&
gt;flags() &
amp; ~
Qt::
ItemIsEnabled);
item1-&
gt;setFlags(item1-&
gt;flags() &
amp; ~
Qt::
ItemIsEnabled);
}
locationsTable-&
gt;setItem(row, 0
, item0);
locationsTable-&
gt;setItem(row, 1
, item1);
}
}
locationsTable-&
gt;setUpdatesEnabled(true
);
}