// Helper class
// QtSettingsHelper.h
#include <QVariant>
class CQtSettingsHelper
{
private:
virtual ~CQtSettingsHelper() = 0;
public:
};
// QtSettingsHelper.cpp
#include "SAQtSettingsHelper.h"
#include <QCoreApplication>
#include <QSettings>
{
settings.beginGroup( group );
QVariant value
= settings.
value( key, defValue
);
settings.endGroup();
return value;
}
{
settings.beginGroup( group );
settings.setValue( key, value );
settings.endGroup();
}
// Example how to use to save / restore window sizes
// Mainindow.h:
class CMainWindow
{
Q_OBJECT
public:
CMainWindow
( QWidget * pParent
= 0 );
~CMainWindow();
protected:
private:
void restoreSettings();
void saveSettings();
};
// MainWindow.cpp
{
saveSettings();
pEvent->accept();
}
void CMainWindow
::showEvent( QShowEvent * pEvent
) {
restoreSettings();
pEvent->accept();
}
static const int sStateVersion = 1000; // Used for versioning state information
void CMainWindow::restoreSettings()
{
QString path
= QStandardPaths
::writableLocation( QStandardPaths
::DocumentsLocation );
resize
( CQtSettingsHelper
::ReadValue( "MainWindow",
"Size",
QSize( 800,
800 ) ).
toSize() );
move
( CQtSettingsHelper
::ReadValue( "MainWindow",
"Position",
QPoint( 50,
50 ) ).
toPoint() );
restoreState( CQtSettingsHelper::ReadValue( "MainWindow", "State" ).toByteArray(), sStateVersion );
}
void CMainWindow::saveSettings()
{
CQtSettingsHelper::WriteValue( "MainWindow", "Size", size() );
CQtSettingsHelper::WriteValue( "MainWindow", "Position", pos() );
CQtSettingsHelper::WriteValue( "MainWindow", "State", saveState( sStateVersion ) );
}
// Helper class
// QtSettingsHelper.h
#include <QVariant>
class QString;
class CQtSettingsHelper
{
private:
virtual ~CQtSettingsHelper() = 0;
public:
static QVariant ReadValue( const QString & group, const QString & key, const QVariant & defValue = QVariant() );
static void WriteValue( const QString & group, const QString & key, const QVariant & value );
};
// QtSettingsHelper.cpp
#include "SAQtSettingsHelper.h"
#include <QCoreApplication>
#include <QSettings>
QVariant CQtSettingsHelper::ReadValue( const QString & group, const QString & key, const QVariant & defValue )
{
QSettings settings( QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName() );
settings.beginGroup( group );
QVariant value = settings.value( key, defValue );
settings.endGroup();
return value;
}
void CQtSettingsHelper::WriteValue( const QString & group, const QString & key, const QVariant & value )
{
QSettings settings( QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName() );
settings.beginGroup( group );
settings.setValue( key, value );
settings.endGroup();
}
// Example how to use to save / restore window sizes
// Mainindow.h:
class CMainWindow
: public QMainWindow
{
Q_OBJECT
public:
CMainWindow( QWidget * pParent = 0 );
~CMainWindow();
protected:
virtual void closeEvent( QCloseEvent * pEvent );
virtual void showEvent( QShowEvent * pEvent );
private:
void restoreSettings();
void saveSettings();
};
// MainWindow.cpp
void CMainWindow::closeEvent( QCloseEvent * pEvent )
{
saveSettings();
pEvent->accept();
}
void CMainWindow::showEvent( QShowEvent * pEvent )
{
restoreSettings();
pEvent->accept();
}
static const int sStateVersion = 1000; // Used for versioning state information
void CMainWindow::restoreSettings()
{
QString path = QStandardPaths::writableLocation( QStandardPaths::DocumentsLocation );
resize( CQtSettingsHelper::ReadValue( "MainWindow", "Size", QSize( 800, 800 ) ).toSize() );
move( CQtSettingsHelper::ReadValue( "MainWindow", "Position", QPoint( 50, 50 ) ).toPoint() );
restoreState( CQtSettingsHelper::ReadValue( "MainWindow", "State" ).toByteArray(), sStateVersion );
}
void CMainWindow::saveSettings()
{
CQtSettingsHelper::WriteValue( "MainWindow", "Size", size() );
CQtSettingsHelper::WriteValue( "MainWindow", "Position", pos() );
CQtSettingsHelper::WriteValue( "MainWindow", "State", saveState( sStateVersion ) );
}
To copy to clipboard, switch view to plain text mode
Bookmarks