我需要以某種方式修改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