2011-09-08 31 views

回答

1

沒有魔術棒可以滿足您的要求。特別是,當程序重新加載時,所有來自前一次運行的指針將無效。你不能通過保存內存轉儲來解決這個問題,因爲程序本身可能會被加載到不同的地址。您必須明確地保存和恢復所有相關的數據結構。

0

您可以查看序列化變量,然後在加載時添加額外的函數以檢查文件是否存在,以及是否存在加載它們。您應該查看QDataStream類以及它提供的功能。

+0

根據原始海報的需求,QSettings可能足以滿足需要,因此可能不需要處理QDataStream和QFile。 QSettings可以處理各種基本數據類型以及各種Qt類型(如QRect等)。 – Xenakios

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()調用應小部件已建成之後進行,不要把它稱爲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(); 
} 

我打這個大多來自記憶,所以代碼不能保證編譯並直接工作,但希望它給你如何去了解它的想法。

相關問題