2011-09-08 6 views
0

큰 빨간 버튼을 사용하여 언제든지 종료 할 수 있도록 10 개의 프로그램 소스를 수정해야합니다. 나중에 다시 시작할 때 중단했던 동일한 위치에서 작업 할 수 있습니다. 기존의 모든 변수를 가져 오는 방법이 있습니까?기존 변수를 모두 가져 와서 Qt의 QSettings에 저장할 수 있습니까?

+0

내가 내 자신의 개체에는 직렬화 할 수 사용하는 것입니다? – user907406

답변

1

당신이 필요로하는 마술 지팡이가 없습니다. 특히, 프로그램이 다시로드되면 이전 실행에서 모든 포인터가 유효하지 않게됩니다. 그리고 프로그램 자체가 아마도 다른 주소에로드되기 때문에 메모리 덤프를 저장하여이 문제를 해결할 수는 없습니다. 모든 관련 데이터 구조를 명시 적으로 저장하고 복원하는 작업을해야합니다.

0

변수를 직렬화 한 다음 추가 기능을 추가하여 파일 존재 여부를 확인하고 다시로드 할 수 있습니다. QDataStream 클래스와이 기능이 제공하는 기능에 대해 살펴보아야합니다 .

+0

원본 포스터의 필요에 따라 QSettings로 필요한 작업을 수행 할 수 있으므로 QDataStream 및 QFile을 처리 할 필요가 없습니다. QSettings는 QRect 등 다양한 Qt 유형뿐만 아니라 다양한 기본 데이터 유형을 처리 할 수 ​​있습니다. – Xenakios

0

다른 답변에서 설명한 것처럼 C++에서는 프로그램의 상태/변수 직렬화 및 비 직렬화를 포괄적으로 처리 할 수있는 메커니즘이 없습니다. 모든 방법은 그러한 기능을 명시 적으로 다루는 수기 코드를 필요로합니다.

예를 들어 QSet에 관해서는 QMainWindow에 2 가지 메소드를 추가 할 수 있습니다 (프로그램의 필요한 상태에 액세스 할 수있는 클래스가 무엇이든간에 사용하고 있다고 가정 할 때). 상태를 저장하고 복원합니다. QSettings 개체를 사용합니다. 같은

뭔가 다음 restoreGeometry 잘 작동하는 위젯이 구축 된 후, loadStateFromSettings() 호출이되어야한다고

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 
} 

주, 위젯 클래스 생성자 자체를 호출하지 않습니다. 코드를 컴파일하고 직접 작동하지 않을 수있다, 그래서 내가 주로 메모리에서이 입력 한

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(); 
} 

, 그러나 희망 그것은 당신에게 그것에 대해 이동하는 방법에 대한 아이디어를 제공합니다 : 귀하의 main() 함수는 뭔가처럼 될 수있다 .

-1
  1. 해결책은 QSettings
  2. 을 사용하는 것입니다 Sessions
  3. 또 다른 솔루션을 사용하는 것입니다 그리고 세 번째 솔루션은 Serialization
관련 문제