我需要以某种方式修改10个程序的来源,以便我可以随时使用大红色按钮退出它们,之后,当我重新启动它们时,从我离开的同一个地方工作。是否有获取所有现有变量的方法?
答案 0 :(得分:1)
没有魔杖可以满足您的需求。特别是,当重新加载程序时,前一次运行的所有指针都将无效。而且你无法通过保存内存转储来解决这个问题,因为程序本身可能会被加载到不同的地址。您必须投入明确保存和恢复所有相关数据结构的工作。
答案 1 :(得分:0)
您可以查看序列化变量,然后在加载时添加额外函数以检查文件是否存在以及是否存在将它们加载回来。您应该查看QDataStream类及其提供的功能。
答案 2 :(得分:0)
正如其他答案所解释的那样,C ++中根本没有可用的机制可以一般地处理序列化和反序列化程序的状态/变量。所有方法都涉及手写代码以明确处理此类功能。
关于QSettings,你可以在你的QMainWindow中添加2个方法(假设你使用的是一个,如果不是,那么任何类都可以访问程序所需的状态)使用它来保存和恢复状态一个QSettings对象。
类似的东西:
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
}
请注意,要使restoreGeometry正常工作,应在构建窗口小部件后进行loadStateFromSettings()调用,不要在窗口小部件类构造函数本身中调用它。你的main()函数可能是这样的:
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();
}
我主要是从内存中输入,所以代码不能保证直接编译和工作,但希望它可以让你知道如何去做。
答案 3 :(得分:-1)