Can I get all existing variables and save them to QSettings in qt?
I need to modify sources of 10 programs in a way so that I could quit them at any tim开发者_运维百科e using big red button and later, when I restart them, to work from the same place I left off. Is there a method of getting all existing variables?
There is no magic wand that will do what you require. In particular, when the program is reloaded all the pointers from the previous run will be invalid. And you can't get around this by saving a memory dump, because the program itself will probably be loaded at a different address. You have to put in the work of explicitly saving and restoring all the relevant data structures.
You could look at serialising the variables and then adding an extra function on load to check for the presence of a file and if it exists loading them back in. You should have a look at the QDataStream class and what functionality it provides.
As explained in the other answers, there's simply no mechanism available in C++ that could generically deal with serializing and deserializing the states/variables of your program. All methods will involve hand written code to deal explicitly with such functionality.
Spesifically about QSettings, you could for example add 2 methods into your QMainWindow (assuming you are using one, if not, whatever class is the one that has access to the needed states of your program) that save and restore the states using a QSettings object.
Something like :
void MainWindow::saveStateToSettings() // QMainWindow already has method saveState() to store dockwidgets and toolbars locations and visibility, so don't use that function name for this
{
QSettings settings; // the QSettings default constructor can be made to have default parameters like shown in the main() function code below
settings.setValue("mywindowgeometry",saveGeometry()); // QWidget::saveGeometry is the recommended way to serialize the position, size and monitor number etc of QWidget
settings.setValue("myvariable1",m_myVariable1); // m_myVariable1 could be any of various basic C++ or Qt value datatypes, like int, float, QString, QRect, QByteArray etc, let's assume here it is a double floating point number. DON'T store pointers using this, serializing pointers is almost always useless and/or dangerous
settings.setValue("checkbox1checked",ui->checkBox->isChecked()); // store a bool
settings.setValue("plaintextedit1text",ui->plainTextEdit->toPlainText()); // store a QString
// write similar code as above to save all other needed state
// that's all there is to it to save the state!
}
void MainWindow::loadStateFromSettings()
{
QSettings settings;
restoreGeometry(settings.value("mywindowgeometry").toByteArray()); // QWidget::restoreGeometry restores the widget geometry from data that was generated previously with QWidget::saveGeometry
m_myVariable1=settings.value("myvariable1",0.5).toDouble(); // the 0.5 sets a default value if the QSettings instance is missing the variable or there's some other problem with the QSettings instance
ui->checkBox->setChecked(settings.value("checkbox1checked",true).toBool()); // again, the "true" value will be used in case of problem with QSettings
ui->plainTextEdit->setPlainText(settings.value("plaintextedit1text").toString()); // no value as the default value "magically" gives an empty QString
}
Note that for the restoreGeometry to work right, the loadStateFromSettings() call should be made after the widget has been constructed, don't call it in the widget class constructor itself. Your main() function could be something like :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSettings::setDefaultFormat(QSettings::IniFormat); // now QSettings default constructor anywhere in the program code creates a QSettings object that uses ini-files. Note that on Windows you could use the registry and on Mac plist-files. Read the QSettings documentation for more on this
QApplication::setApplicationName("MyApplication"); // you should set this for your app object so QSettings can store the settings for your app in a location that can be identified by that name
QApplication::setOrganizationName("MyName"); // you should set this for your app object, the organization name is effectively your "company" name, and it makes QSettings store the settings for your app(s) in a location that can be identified by that name
MainWindow w;
w.loadStateFromSettings();
w.show();
return a.exec();
}
I typed this mostly from memory, so the code isn't guaranteed to compile and work directly, but hopefully it gives you an idea on how to go about it.
- A solution would be to use Sessions
- Another solution would be to use QSettings
- And third solution would be to use Serialization
精彩评论