2011-09-08 33 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(); 
} 

我打这个大多来自记忆,所以代码不能保证编译并直接工作,但希望它给你如何去了解它的想法。

相关问题