我需要以某种方式修改10个程序的源代码,以便我可以随时使用大红色按钮退出它们,之后,当我重新启动它们时,可以从我离开的同一个地方开始工作。是否有一种获取所有现有变量的方法?我可以获取所有现有变量并将它们保存到qSettings中吗?
回答
没有魔术棒可以满足您的要求。特别是,当程序重新加载时,所有来自前一次运行的指针将无效。你不能通过保存内存转储来解决这个问题,因为程序本身可能会被加载到不同的地址。您必须明确地保存和恢复所有相关的数据结构。
您可以查看序列化变量,然后在加载时添加额外的函数以检查文件是否存在,以及是否存在加载它们。您应该查看QDataStream类以及它提供的功能。
根据原始海报的需求,QSettings可能足以满足需要,因此可能不需要处理QDataStream和QFile。 QSettings可以处理各种基本数据类型以及各种Qt类型(如QRect等)。 – Xenakios
正如其他答案中所解释的那样,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()调用应小部件已建成之后进行,不要把它称为Widget类的构造函数本身。你的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();
}
我打这个大多来自记忆,所以代码不能保证编译并直接工作,但希望它给你如何去了解它的想法。
- 一种解决方案是使用Sessions
- 另一种解决方案是使用QSettings
- 而第三个方案是使用Serialization
- 1. 从数据库获取所有变量并将它们存储为会话变量会不好吗?
- 2. 我如何将它保存到一个变量中,以便我可以将它保存到文件中
- 3. 我可以使用array.join获取数组中的所有整数并将它们合并为一个int吗?
- 4. 我们可以从HBase表中获取所有列名吗?
- 5. 获取所有家长,并将它们存储在字符串
- 6. 保存工作区 - 将所有变量保存到文件。 Python没有它)
- 7. 这样做可以吗?将所有变量存储在datagridview中
- 8. 我们可以从slickgrid获取所有的值吗?
- 9. 我可以将HTML5画布绘图保存到变量吗?
- 10. 我可以在HTML.PartialRender()中有变量吗?
- 11. 我可以看到所有临时变量的列表吗?
- 12. 我们可以在接口中有成员变量吗?
- 13. 我们可以通过hiredis保存Redis中的C结构吗?我可以保存但不能得到它
- 14. 获取表中的行号并将其保存到php变量
- 15. WPF - 可以通过它们的值获取所有UI控件
- 16. 在构造函数中,我可以获取成员的地址并依赖它们保持不变吗?
- 17. 我可以从现有变量中设置PHP类属性吗?
- 18. 将变量保存为onsavedinstance并确定它们是否为空
- 19. URL变量和PHP重定向,它们可以被缓存吗?
- 20. 我可以将关联数组提取到变量中吗?
- 21. 我们可以用一个变量将一个类保存在一个变量中吗?
- 22. 我们可以将字符串保存为OpenTSDB中的值吗?
- 23. 获取两列的总和并将其保存到变量
- 24. Django queryset。将所有行保存到变量并删除表中的行
- 25. AVAudioRecorder:我们可以记录而不保存到文件中吗?
- 26. 我可以将现有的Subversion存储库移动到新的父存储库(并保留历史记录)吗?
- 27. 我可以将AVAudioPlayer引用存储在NSMutableArray中并随意播放它们吗?
- 28. 如何保存附加到变量然后将它们转换为PHP,以便您可以将它们保存在数据库中?
- 29. php抓取URL中的所有GET变量,并将它们作为POST变量发送回相同的URL
- 30. 可以获取gmail的所有附件列表并下载它?
我能连载自己的对象吗? – user907406